How to iterate through json and add data in to array? - ios

Here is how I iterate through my Json "data":
[myObject removeAllObjects];
NSDictionary *dic = jsonData[#"data"];
for (NSArray *ar in dic) {
[myObject addObject:ar];
}
Here is NSLog for myObject:
myObject: (
23,
22,
21
)
How can I iterate through all "data" (in 21,22,23 etc.) to add info about companies in to myObject array?
My Json structure:
{
"data": {
"21": {
"id": "21",
"company_id": "17",
"company_name": "Company 1",
"logo_url": "http://www.company.com/1.png",
"status": "2",
"allow_name": "N",
"allow_address": "N",
"allow_email": "N",
"allow_phone": "N",
"allow_birthday": "N",
"allow_gender": "N",
"created": false
},
"22": {
"id": "22",
"company_id": "5",
"company_name": "Company 2",
"logo_url": "http://www.company.com/2.png",
"status": "2",
"allow_name": "N",
"allow_address": "N",
"allow_email": "N",
"allow_phone": "N",
"allow_birthday": "N",
"allow_gender": "N",
"created": false
}
},
"success": 1
}

You should append the following sytax in your dictionary.
[valueForKey:#"company_name"]
Check the following tutorial: http://iosdevtips.co/post/80598241763/valueforkey-nsarray

NSMutableArray *aryData = [NSMutableArray new];
for (int i=0; i<myObject.count; i++) {
[aryData addObject:myObject[i]];
}

Related

How to find key in a NSDictionary object array 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"];

Get entity state before saveChange in breeze js

I am working with mvc with breeze js. when i delete some record from specific entity and when i send data to controller that for causes i need specificed deleted record and its state from JObject. I have a JObject like this
{
"entities": [
{
"StageDetailID": 7,
"EventID": 1,
"StageNUmber": "Stage 1",
"TypeOfStage": "Primary ",
"Distance": "100",
"IsRadioRoadMap": "N",
"IsRadioCourseApproval": "N",
"IsRadioTrafficControl": "N",
"IsRadioPoliceAssistance": "N",
"IsNeutralSupport": "N",
"IsKOMQOM": "N",
"IsSprints": "N",
"IsFeedStations": "N",
"IsRoadClosures": "N",
"IsDetours": "N",
"IsSpeedReductions": "N",
"entityAspect": {
"entityTypeName": "StageDetail:#EventManagement.Domain.Model",
"defaultResourceName": "StageDetails",
"entityState": "Deleted",
"originalValuesMap": {},
"autoGeneratedKey": {
"propertyName": "StageDetailID",
"autoGeneratedKeyType": "Identity"
}
}
}
],
"saveOptions": {}
}
How can i get the Entity StageDetail and Entity State from this JObject. Thanks
In your controller define before save entity like below
public bool BeforeSaveEntity(EntityInfo info)
{
}
and it ill hit when ever "Save changes" method is invoked, there you delete records from your entity.
For reference go through the following link
Breeze before save entites

How to get extended json response from ASP.net controller method

I am working on ASP.net MVC Applciation
I am facing difficulty in preparing json response to return from a controller action method.
I need some thing like this:
{
"cityMap": {"11": "Chennai", "12": "Mumbai", "13": "Delhi"},
"rows": [
{ "SID": "1", "SNAME": "ABC", "CITY": "11" },
{ "SID": "2", "SNAME": "XYZ", "CITY": "12" },
{ "SID": "3", "SNAME": "ACX", "CITY": "13" },
{ "SID": "4", "SNAME": "KHG", "CITY": "13" },
{ "SID": "5", "SNAME": "ADF", "CITY": "12" },
{ "SID": "6", "SNAME": "KKR", "CITY": "11" }
]
}
I have cityMap values in the sortedlist names slLocations
I have row values in the form of list liStudents
I am using the following syntax to send the json response:
return JSON(new { rows=liStudents},jsonRequestBehaviour.AllowGet)
By using the above syntax , i am getting the json response like this:
{
"rows": [
{ "SID": "1", "SNAME": "ABC", "CITY": "11" },
{ "SID": "2", "SNAME": "XYZ", "CITY": "12" },
{ "SID": "3", "SNAME": "ACX", "CITY": "13" },
{ "SID": "4", "SNAME": "KHG", "CITY": "13" },
{ "SID": "5", "SNAME": "ADF", "CITY": "12" },
{ "SID": "6", "SNAME": "KKR", "CITY": "11" }
]
}
Please help on how to extend json response to above by using liStudents and liLocations
Simply add the desired cityMap property to the anonymous object that you are passing to the view:
var model = new
{
cityMap = slLocations,
rows = liStudents,
}
return JSON(model, JsonRequestBehaviour.AllowGet);
This obviously assumes that the slLocations variable is a SortedList<string, string>.

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"];

Json Array - Blackberry

I try to get data from json array
String username = object.getJSONObject("items").getString("f");
But it gives me a error
"items.f" not found.
Below is the Json Array.
{
"items": [
{
"s": "0",
"f": "monems",
"m": "ustad"
},
{
"s": "0",
"f": "monems",
"m": "There?"
},
{
"s": "0",
"f": "monems",
"m": "What's going on ?"
},
{
"s": "2",
"f": "monems",
"m": "Sent at 4:03AM May 20th"
}
]
}
try this -
JSONArray items=object.getJSONArray("items");
for(int i=1;i<=items.length();i++){
JSONObject jsonObj=items.getJSONObject(i);
String s=jsonObj.getString("f");
}

Resources