How to find key in a NSDictionary object array ios - ios

I have a NSMutableArraylike this.
[
{
"Employee": {
"EmployeeCode": 17125,
"DisplayName": "MI0000026 - ABC",
"DisplayName2": "ABC2",
"Initials": "W. S. S.",
"Surname": "CCC",
"FirstName": "ABC",
"MiddleName": "CDE",
"LastName": "",
"FullName": "ABC EMPLOYEE",
},
"LeaveDetail": {
"LeaveDetailId": 21,
"LeaveEntryCode": 16,
"RequestId": 20,
"EmployeeCode": 17125,
"LeaveYear": 2016,
"LeaveTypeCode": 1,
"BaseType": "ess",
"LeaveDate": "2016-09-05T00:00:00",
}
}
]
there are several objects in this array which has these kind of dictionaries. I want to find all the objects in this array which has "LeaveDate": "2016-09-05T00:00:00" this LeaveDate key is inside the LeaveDetail dictionary. How can I find all the objects which has LeaveDate as "2016-09-05T00:00:00"
EDIT
NSPredicate *predicate=[NSPredicate predicateWithFormat:#"LeaveDate CONTAINS[cd] %#",dm.strClickedDate];
But, I don't get any result.
Please help me.
Thanks

NSDictonary *dict = [yourMutableArray[0]];//get dict in array with index = 0
NSLog(#"%#",dict);
//*Log dict will show:
{
"Employee": {
"EmployeeCode": 17125,
"DisplayName": "MI0000026 - ABC",
"DisplayName2": "ABC2",
"Initials": "W. S. S.",
"Surname": "CCC",
"FirstName": "ABC",
"MiddleName": "CDE",
"LastName": "",
"FullName": "ABC EMPLOYEE",
},
"LeaveDetail": {
"LeaveDetailId": 21,
"LeaveEntryCode": 16,
"RequestId": 20,
"EmployeeCode": 17125,
"LeaveYear": 2016,
"LeaveTypeCode": 1,
"BaseType": "ess",
"LeaveDate": "2016-09-05T00:00:00",
}
}
*//
NSDictionay *leaveDetailDict = [dict objectForKey:#"LeaveDetail"];//get dict have key LeaveDetail
NSLog(#"%#",leaveDetailDict[#"LeaveDate"];

Related

Schema definition in Swagger

I'm very new to OpenAPI and I'm using http://editor.swagger.io to design an API.
I'm stuck in Schema with a JSON looking like following
{
"CORRELATION_ID": "10",
"CONTROL":
{
"DAS_IS_RECIPIENT": "123",
"DOCTPYE": "ert",
"PROCESS_INDICATOR": "nord"
},
"HEADER":
{
"ID": "456",
"INVOICE_NUMBER": "678",
"DMS_DOC_ID": "876",
"INVOICE_DATE": "10082020"
},
"ITEMS": [
{
"SHORT_TEXT": "123",
"LSTAR": 0,
"QUANTITY": "23"
},
{
"SHORT_TEXT": "456",
"LSTAR": 234,
"QUANTITY": "21"
}
],
"DEBITOR":
{
"ID": "444",
"FIRSTNAME": "nick",
"LASTNAME": "cantre"
},
"CREDITOR":
{
"ID": "454",
"FIRSTNAME": "ava",
"LASTNAME": "pierre"
}
}
How to create a schema according to this JSON structure?

I m getting json data for title in UTF 8 format, then how solve it?

I am getting this,
"title": "=?UTF-8?Q?=E2=80=9CUber=E2=80=99s_Complicit_Board=E2=80=9D_published_?= =?UTF-8?Q?in_Monday_Note_by_Jean-Louis_Gass=C3=A9e?=", //here
I m receiving following josn data,
{
"updated_at": "2017-06-27 13:45:23",
"user_name": "noreply#medium.com",
"first_name": "",
"last_name": "",
"email": "noreply#medium.com",
"profile_pic": "https://secure.gravatar.com/avatar/1f98a43dd7d8f9568b557c7f03fe854e?s=80&r=g&d=identicon",
"ticket_number": "AAAA-0000-0001",
"id": 2,
"title": "=?UTF-8?Q?=E2=80=9CUber=E2=80=99s_Complicit_Board=E2=80=9D_published_?= =?UTF-8?Q?in_Monday_Note_by_Jean-Louis_Gass=C3=A9e?=",
"created_at": "2017-06-27 13:45:23",
"department_name": "Support",
"priotity_name": "Low",
"priority_color": "#00a65a",
"sla_plan_name": "Low",
"help_topic_name": "Support query",
"ticket_status_name": "Open",
"department_id": 1,
"user_dpt": null,
"attachment": 0,
"overdue_date": "2017-06-27 18:45:23"
},
{
"updated_at": "2017-06-27 13:45:19",
"user_name": "help#teamtreehouse.com",
"first_name": "Treehouse",
"last_name": "",
"email": "help#teamtreehouse.com",
"profile_pic": "https://secure.gravatar.com/avatar/84541406cf3c552347f8e31cef7d32ce?s=80&r=g&d=identicon",
"ticket_number": "AAAA-0000-0000",
"id": 1,
"title": "=?UTF-8?Q?=C2=A0New_skills", // here also
"created_at": "2017-06-27 13:45:19",
"department_name": "Support",
"priotity_name": "Low",
"priority_color": "#00a65a",
"sla_plan_name": "Low",
"help_topic_name": "Support query",
"ticket_status_name": "Open",
"department_id": 1,
"user_dpt": null,
"attachment": 0,
"overdue_date": "2017-06-27 18:45:19"
}
]
}
That is the "encoded-word" encoding from RFC 2047.
There is an online decoder here: http://dogmamix.com/MimeHeadersDecoder/
libetpan can decode it (see mailmime_encoded_word_parse
in https://github.com/dinhviethoa/libetpan/blob/master/src/low-level/mime/mailmime_decode.c). That is pretty heavyweight if you only care about this field. You could write a decoder yourself based on the RFC instead.
You need to decode the title string, so check the below code.
NSString *encodedString =#"?UTF-8?Q?=C2=A0New_skills";
NSData *decodedData = [[NSData alloc] initWithBase64EncodedString: encodedString options:0];
NSString *decodedString = [[NSString alloc] initWithData:decodedData encoding:NSUTF8StringEncoding];
NSLog(#"%#", decodedString);
NSLog(#"Decoded String: %#",decodedString);

Pulling Data from an NSDictionary

So I have an NSDictionary that has a variety of data within it. When printed to the log, it prints like this:
[{"user_id":3016817,"grade":"A","percent":"93","grading_periods":[{"assignments":[{"points":100.0,"grade":"A","score":95.0,"percent":"93","comment":null,"id":3268180},{"points":100.0,"grade":"A","score":90.0,"percent":"93","comment":null,"id":3268181}],"grade":"A","percent":"93","name":"Default"}]},{"user_id":3016818,"grade":"A","percent":"94","grading_periods":[{"assignments":[{"points":100.0,"grade":"A","score":92.0,"percent":"94","comment":null,"id":3268180},{"points":100.0,"grade":"A","score":95.0,"percent":"94","comment":null,"id":3268181}],"grade":"A","percent":"94","name":"Default"}]}]
If I use a formatter online, its a lot more readable and looks something like this:
[
{
"user_id": 3016817,
"grade": "A",
"percent": "93",
"grading_periods": [
{
"assignments": [
{
"points": 100,
"grade": "A",
"score": 95,
"percent": "93",
"comment": null,
"id": 3268180
},
{
"points": 100,
"grade": "A",
"score": 90,
"percent": "93",
"comment": null,
"id": 3268181
}
],
"grade": "A",
"percent": "93",
"name": "Default"
}
]
},
{
"user_id": 3016818,
"grade": "A",
"percent": "94",
"grading_periods": [
{
"assignments": [
{
"points": 100,
"grade": "A",
"score": 92,
"percent": "94",
"comment": null,
"id": 3268180
},
{
"points": 100,
"grade": "A",
"score": 95,
"percent": "94",
"comment": null,
"id": 3268181
}
],
"grade": "A",
"percent": "94",
"name": "Default"
}
]
}
]
My question would be how would I access the value of grade or score for a specific user_id using this dictionary?
Your string represents a NSArray, not a NSDictionary. And it's a JSON string, so you can parse it using NSJSONSerialization:
NSString *jsonString = #"..." // your string here
// Create array from json string
NSArray *jsonArray = [NSJSONSerialization
JSONObjectWithData:[jsonString dataUsingEncoding:NSUTF8StringEncoding]
options:NSJSONReadingMutableContainers
error:Nil];
// Loop to find your user_id
// Because each child of this array is a dictionary
for (NSDictionary *dic in jsonArray) {
if ([dic[#"user_id"] isEqual:#3016817]) { // user_id field is number
// Access what you want
NSString *grade = dic[#"grade"];
// For "score" you must go deeper
// Just remember, [] is array and {} is dictionary
}
}
A simple way to do this would be
for (NSDictionary* d in theArray) {
if ([d[#"user_id"] isEqualToString: u]) {
// do something
}
}
And matt is right, it is an array of dictionaries, no matter what type you declared to be.
You can use below class method of NSJsonSerialization class to create NSArray which will contain all the dictionaries.
+ (id)JSONObjectWithData:(NSData *)data options:(NSJSONReadingOptions)opt error:(NSError **)error;
Once you get the array of dictionaries from JSON, you can do following:
NSArray *filteredjsonArr = [jsonArr filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:#"user_id == %#", #"3016818"]];
NSDictionary *dict = [filtered firstObject];
if (dict != nil) {
NSString *grade = [dict objectForKey:#"grade"];
NSArray *gradingPeriods = [dict objectForKey:#"grading_periods"];
}
To access score and grade for specific assignments, you'll need to drill down further into gradingPeriods array.

Use Json_create always get NULL frome json-string in cocos2dx 2.2.3

My code at follow
CCLOG("info: %s",infoStr.c_str());
const char * buf=infoStr.c_str();
cocos2d::extension::Json* mJs = cocos2d::extension::Json_create(buf);
cocos2d::extension::Json* item = Json_getItem(mJs,"results");
cocos2d::extension::Json* array1 = Json_getItemAt (item, 0);
cocos2d::extension::Json* itemNode = Json_getItem(array1,"version");
float floatValue = itemNode->valuefloat;
CCLOG("floatValue: %f",floatValue);
The result of CCLOG("info: %s",infoStr.c_str()); is at follow:
{
"resultCount": 1,
"results": [
{
"kind": "software",
"features": [],
"supportedDevices": [
"iPhone4S",
"iPadWifi",
"iPadThirdGen4G",
"iPhone5",
"iPadMini4G",
"iPadFourthGen4G",
"iPodTouchThirdGen",
"iPadThirdGen",
"iPad3G",
"iPhone4",
"iPodTouchourthGen",
"iPodTouchFifthGen",
"iPhone-3GS",
"iPad2Wifi",
"iPhone5c",
"iPad23G",
"iPhone5s",
"iPadFourthGen",
"iPadMini"
],
"isGameCenterEnabled": false,
"screenshotUrls": [
"http://a5.mzstatic.com/us/r30/Purple/v4/52/c0/e5/52c0e504-3e37-80a8-f17a-0b49bff6e63b/screen1136x1136.jpeg",
"http://a4.mzstatic.com/us/r30/Purple6/v4/b6/8d/50/b68d500a-b84a-44fe-7fac-a055aa49e2ee/screen1136x1136.jpeg",
"http://a5.mzstatic.com/us/r30/Purple4/v4/30/80/ab/3080ab26-fa55-a69b-480d-a57c803f3b81/screen1136x1136.jpeg"
],
"ipadScreenshotUrls": [],
"artworkUrl60": "http://a288.phobos.apple.com/us/r30/Purple6/v4/e6/89/56/e689561d-2bbc-8769-4325-b2acd91e0cf9/AppIcon57x57.png",
"artworkUrl512": "http://a1062.phobos.apple.com/us/r30/Purple4/v4/1d/8c/2c/1d8c2c76-d441-4d3b-d43c-48e6f7aa98ad/mzl.oympcnqz.png",
"artistViewUrl": "https://itunes.apple.com/us/artist/wei-gao/id777399907?uo=4",
"artistId": 777399907,
"artistName": "wei gao",
"price": 0,
"version": "1.0",
"description": "description",
"currency": "USD",
"genres": [
"Photo & Video",
"Entertainment"
],
"genreIds": [
"6008",
"6016"
],
"releaseDate": "2014-01-02T23:19:32Z",
"sellerName": "w",
"bundleId": "D.IMyPhotos",
"trackId": 77,
"trackName": "ff",
"primaryGenreName": "Photo & Video",
"primaryGenreId": 6008,
"formattedPrice": "Free",
"wrapperType": "software",
"trackCensoredName": "ff",
"languageCodesISO2A": [
"EN"
],
"fileSizeBytes": "14",
"sellerUrl": "http://wwww",
"contentAdvisoryRating": "4+",
"artworkUrl100": "http://a1062.phobos.apple.com/us/r30/Purple4/v4/1d/8c/2c/1d8c2c76-d441-4d3b-d43c-48e6f7aa98ad/mzl.oympcnqz.png",
"trackViewUrl": "https://itunes.apple.com/us/app",
"trackContentRating": "4+"
}
]
}
I get Json* mJs always NULL.I think the string of json is right.I used google but can't get the answer.
Do i use Json_create in the wrong way? Or other reason? A lot of thank!

Parsing Json to get all the contents in one NSArray

That's the content...
[
{
"id": "",
"title": "",
"website": "",
"categories": [
{
"id": "",
"label": ""
}
],
"updated":
},
{
"id": "",
"title": "",
"website": "",
"categories": [
{
"id": "",
"label": ""
}
],
"updated":
}
]
How can I insert every feed source in one array?
NSDictionary *results = [string JSONValue];
NSArray *subs = [results valueForKey:#"KEY"];
Which key I must insert?
THanks
as I can see your structure, you will get out of this JSON-String
NSArray:
[
NSDictionary:
{
NSString: "id",
NSString: "title",
NSString: "website",
NSArray: "categories":
[
NSDictionary:
{
NSString: "id",
NSString: "label"
}
],
NSNumber: "updated"
},
NSDictionary:
{
...
}
]
So you have already an array of "Feeds" at root and you have to itterate them with their index in the array with. For first id i.e. [[myJsonStructure objectAtIndex:0] objectForKey:#"id"];

Resources