JSON parsing results in nscfarray inside nsrray obj c - ios

I am converting JSON formatted data from an MQTT message with
id json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
As a result I got a dictionary and it is as follows
{colorCode = "#0000FF";
label = "Tornado Warning";
locations = (
{
account = 3;
"floor_plan" = (
18
);
id = 11;
label = "Albert Elementary School (ES)";
}
);
msgType = AlertInitiated;
origin = webapp; }
For fetching the floor_plan array, using the following code.
NSDictionary *locations = [dict valueForKey:#"locations"];
NSArray *floor_plan = [locations valueForKey:#"floor_plan"];
I got the floor_plan as:
<__NSArrayI 0x7fa87b5919f0>(
<__NSCFArray 0x7fa87b5af8d0>(
18
)
)
Why NSCFAray is generating inside in NSArray?
Note :- The same json is parsing in android and the floor_plan array is getting as a normal array.

NSArray <NSDictionary <NSString *,id> *> *locations = json[#"locations"];
NSDictionary <NSString *,id> *firstLocation = locations.firstObject;
NSArray *floorPlan = firstLocation[#"floor_plan"];

First make sure locations variable dictionary looks ok...
Then try this:
NSArray *floor_plan = [[NSArray alloc] initWithArray:[locations valueForKey:#"floor_plan"]];

Tried like this and it worked.
NSArray *floor_plan = [[locations valueForKey:#"floor_plan"] firstObject];

Related

Parse array of dictionaries of json in ios

i wanted to parse this json using this way
{"img":[{"id":"44","name":"\u0625\u0628\u0631\u0627\u0647\u064a\u0645 \u0627\u0644\u0639\u0631\u064a\u0641\u064a","type":"\u0627\u0646\u0627 \u0627\u062a\u0639\u0644\u0645","img":"27039_01355548242.png","school":"\u0625\u0628\u0631\u0627\u0647\u064a\u0645 \u0627\u0644\u0639\u0631\u064a\u0641\u064a","isshow":"1","tarteb":"0","date":"29\/1\/2015 01:15","mob":"16544541321"}]}
i wrote this code to parse it
NSArray * array = [[NSArray alloc]init];
array = [NSJSONSerialization JSONObjectWithData:responseObject options:kNilOptions error:nil];
NSDictionary * dic =array[indexPath.row]; //here is error
NSLog(#"%#",dic);
NSDictionary * info = dic[#"img"];
cell.name.text = info[#"name"];
cell.date.text =info[#"date"];
cell.schoolName.text= info[#"school"];
cell.mobile.text= info[#"mob"];
then i got this error
[__NSCFDictionary objectAtIndex :]: unrecognized selector sent to instance 0x7c986db0
NSDictionary *jsonDictionary = [[NSDictionary alloc] init];
jsonDictionary = [NSJSONSerialization JSONObjectWithData:responseObject options:kNilOptions error:nil];
NSArray *imageArray = jsonDictionary[#"img"];
NSDictionary *firstObject = imageArray[0];
cell.name.text = firstObject[#"name"];
cell.date.text =firstObject[#"date"];
cell.schoolName.text= firstObject[#"school"];
cell.mobile.text= firstObject[#"mob"];
and If there are multiple rows in json,Like:
{"img":[{"id":"44","name":"\u0625\u0628\u0631\u0627\u0647\u064a\u0645 \u0627\u0644\u0639\u0631\u064a\u0641\u064a","type":"\u0627\u0646\u0627 \u0627\u062a\u0639\u0644\u0645","img":"27039_01355548242.png","school":"\u0625\u0628\u0631\u0627\u0647\u064a\u0645 \u0627\u0644\u0639\u0631\u064a\u0641\u064a","isshow":"1","tarteb":"0","date":"29\/1\/2015 01:15","mob":"16544541321"},{"id":"45","name":"\u0625\u0628\u0631\u0627\u0647\u064a\u0645 \u0627\u0644\u0639\u0631\u064a\u0641\u064a","type":"\u0627\u0646\u0627 \u0627\u062a\u0639\u0644\u0645","img":"27039_01355548242.png","school":"\u0625\u0628\u0631\u0627\u0647\u064a\u0645 \u0627\u0644\u0639\u0631\u064a\u0641\u064a","isshow":"1","tarteb":"0","date":"29\/1\/2015 01:15","mob":"16544541321"},{"id":"46","name":"\u0625\u0628\u0631\u0627\u0647\u064a\u0645 \u0627\u0644\u0639\u0631\u064a\u0641\u064a","type":"\u0627\u0646\u0627 \u0627\u062a\u0639\u0644\u0645","img":"27039_01355548242.png","school":"\u0625\u0628\u0631\u0627\u0647\u064a\u0645 \u0627\u0644\u0639\u0631\u064a\u0641\u064a","isshow":"1","tarteb":"0","date":"29\/1\/2015 01:15","mob":"16544541321"}]}
then use this approach, if you need to show id:44 in zeroth row,id:45 in first row,id:46 in second row,...Of table view.
//In View Did Load
NSMutableArray *tableDataSource = [NSMutableArray new];
for (int i = 0; i < imageArray.count; i++)
{
NSDictionary *rowDataDictionary = imageArray[i];
[tableDataSource addObject:rowDataDictionary];
}
//In Cell For Row
NSDictionary *rowData = tableDataSource[indexPath.row];
cell.name.text = rowData[#"name"];
cell.date.text =rowData[#"date"];
cell.schoolName.text= rowData[#"school"];
cell.mobile.text= rowData[#"mob"];
JsonObjectWithData Returns Dictionary, and you should parse the data according to structure of JSON.
Hope this Helps,
As it shown above, JsonObjectWithData returns NSDictionary object, and you should use valueForKey method for taking value which you need.
NSDictionary *jsonDictionary = [[NSDictionary alloc] init];
jsonDictionary = [NSJSONSerialization JSONObjectWithData:responseObject options:kNilOptions error:nil];
NSMutableArray *valueForImgLabel = [jsonDictionary valueForKey:#"img"];
If you have too much value for one element in your JSON Response object, like this ;
{
Name = "Mr. Hope";
Gender = "Male";
Age = 29;
ImageList = (
{
Name = "Front side";
Image = "11111.JPG";
Order = 1;
Date = "07/07/2014";
},
{
Name = "Left Side";
Image = "22222.JPG";
Order = 1;
Date = "10/28/2015";
});
}
You can take all ImageList elements,convertibly, using valueForkey like this;
NSMutableArray *imageListInJSONFile = [jsonDictionary valueForKey:#"ImageList"][0];

How to get JSON data parse from Arrays of dictionary iOS

list = ({
clouds = 24;
speed = "4.31";
temp = {
day = "283.84";
eve = "283.84";
night = "283.84";
};
}),
Please can anyone tell me what am I doing wrong - I want to display list-->temp-->day value in table first I am trying to get data in an array which is terminating.
Here is my code am I doing any wrong
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:dataBuffer options:-1 error:nil];
NSLog(#"%#",json);
NSMutableDictionary * list = [json objectForKey:#"list"];
NSMutableArray *arrays = [[NSMutableArray alloc]initWithCapacity:0];
for (NSDictionary *lists in [list allValues]) {
[arrays addObject:[list valueForKey:#"temp"]];
}
If you want to access day then use below line,
NSString *day = [json valueForKeyPath:#"list.temp.day"];
Your list is an array, so if you want to do your things without changing much, you can replace:
NSMutableDictionary * list = [json objectForKey:#"list"];
With:
NSMutableDictionary * list = [[json objectForKey:#"list"] objectAtIndex:0];

Json Response parsing

I got json reponse in the following way...i have tried to parse in many ways all went ruin.
dic:
(
{
events = {
id = 1;
name = "Event One";
};
},
{
events = {
id = 2;
name = "Test 2";
};
},
{
events = {
id = 12;
name = "vivek 11";
};
},
)
NSDictionary *jsonDictionaryResponse = [response JSONValue];
NSString *name=[[[jsonDictionaryResponse objectForKey:#"events"]objectAtIndex:0]valueForKey:#"name"];
json response:
Login response :[{"events":{"id":"1","name":" Event
One"}},{"events":{"id":"2","name":"Test
2"}},{"events":{"id":"12","name":"vivek
11"}},{"events":{"id":"13","name":"Baby's Day
out"}},{"events":{"id":"15","name":"Childrens
Day"}},{"events":{"id":"16","name":"event
two"}},{"events":{"id":"17","name":"Test
Creattion"}},{"events":{"id":"29","name":"Susan
Test"}},{"events":{"id":"30","name":"Summer
Holidays"}},{"events":{"id":"38","name":"Event
7"}},{"events":{"id":"69","name":"vivek event for
tests"}},{"events":{"id":"102","name":"chinees food mela"}}]
first transform your response string to NSData. then try this:
NSError *error;
NSArray *jSONArray = [NSJSONSerialization JSONObjectWithData:jsonData options:kNilOptions error:&error];
for (NSDictionary *dict in jSONArray) {
NSDictionary *event = [dict objectForKey=#"events"];
NSString *name = [event objectForKey:#"name"];
....
}
The easiest way to understand a JSON object in Objective-C is to understand how it breaks things up into Arrays and Dictionaries. Every time you see a "[" think Array. Every time you see a "{" think Dictionary.
An Array can have Dictionary or other Array objects as part of the collection and a Dictionary can have Array or more Dictionary objects which can contain more Arrays or Dictionaries.
If you remember that "[ ]" means Array and "{ }" means Dictionary, you will know JSON in Objective-C.
Check that result coming as JSON. it is not aDictionary check it
here
PLease find the code here
NSArray *jsonArrayResponse = [response JSONValue];
NSDictionary *firstDic = [jsonArrayResponse objectAtIndex:0];
NSDictionary *secondDic = [firstDic objectForKey:#"events"];
NSLog(#"The values in the events dictioanry is %# ",[secondDic allValues]);
NSString *stringNAme = [secondDic objectForKey:#"id"];

how to get string removing parameters [duplicate]

This question already has answers here:
Remove characters from NSString?
(6 answers)
Closed 8 years ago.
I am getting string from json dictionory but result string is in brackets, i have to get string without backets
code is
jsonDictionary = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&error];
NSDictionary *dictResult = [jsonDictionary objectForKey:#"result"];
NSDictionary *dictPronunciations = [dictResult valueForKey:#"pronunciations"];
NSDictionary *dictAudio = [dictPronunciations valueForKey:#"audio"];
NSString *strMp3Path = [dictAudio valueForKey:#"url"];
NSLog(#"str mp3 path %#",strMp3Path);
and result is
(
(
"/v2/dictionaries/assets/ldoce/gb_pron/abate0205.mp3"
)
)
I want to get /v2/dictionaries/assets/ldoce/gb_pron/abate0205.mp3 as a string without brackets. Please help...
The object you are logging is not a NSString instance. it is a string inside an array in an array.
try:
NSLog(#"str mp3 path %#",strMp3Path[0][0]);
if this prints as desired, the object dictAudio holds with the key url is an array, with an array. you should fix that where ever you stick it into the dictionary.
Try with following code:
NSMutableArray *myArray = [dictAudio valueForKey:#"url"];
NSString *myStr = [[myArray objectAtIndex:0] objectAtIndex:0];
NSLog(#"%#", myStr);
Use this code. If your values are multiple from json then the value can be added one by one without braces :
NSMutableArray *dictPronunciations = [dictResult valueForKey:#"pronunciations"];
NSMutableArray *arrayPronunciations = [[NSMutableArray alloc] init];
for (int i = 0; i< [dictPronunciations count]; i++)
{
NSString *string = [dictPronunciations objectAtIndex:i];
NSLog(#"String = %#",string);
[arrayPronunciations addObject:string];
}
NSLog(#"Array Pronounciations = %#",arrayPronunciations);

using json, read contents in objective c

I'm learning a very basic method to download data from a weather api.
Basically trying to follow a tutorial.
Using the URL, I am able to download the data in JSON format into a dictionary. Then put into an array.
My question now is how do I read the particular value of an item in the array.
For example, when I do an NSLOG of the array I get the following... I only cut/paste a couple as there are 55 items.
So my question is how do I grab a particular value our of this array?
2013-03-18 14:37:57.576 LocalWeatherV3[1220:c07] loans: {
UV = 2;
"dewpoint_c" = "-4";
"dewpoint_f" = 24;
"dewpoint_string" = "24 F (-4 C)";
"display_location" = {
city = "Jersey City";
country = US;
"country_iso3166" = US;
elevation = "47.00000000";
full = "Jersey City, NJ";
latitude = "40.75180435";
longitude = "-74.05393982";
state = NJ;
"state_name" = "New Jersey";
zip = 07097;
};
estimated = {
};
"feelslike_c" = 2;
"feelslike_f" = 35;
"feelslike_string" = "35 F (2 C)";
"forecast_url" = "http://www.wunderground.com/US/NJ/
here is a piece of the .m
- (void)fetchedData:(NSData *)responseData {
//parse out the json data
NSError* error;
NSDictionary* json = [NSJSONSerialization
JSONObjectWithData:responseData //1
options:kNilOptions
error:&error];
NSArray* latestLoans = [json objectForKey:#"current_observation"]; //2
NSLog(#"loans: %#", latestLoans); //3
// 1) Get the latest loan
//NSDictionary* loan = [latestLoans objectAtIndex:1];
NSInteger counter = [latestLoans count];
thanks in advance!!
so when I do this
NSDictionary* json = [NSJSONSerialization
JSONObjectWithData:responseData //1
options:kNilOptions
error:&error];
and i mouse over the local watch, I see
json NSDictionary * 0x08d62d40
[0] key/value pair
key id 0x08d61cf0
value id 0x08d62100
[1] key/value pair
key id 0x08d62150
value id 0x08d633a0
then i do
NSArray* latestLoans = [json objectForKey:#"current_observation"]; //2
NSLog(#"loans: %#", latestLoans); //3
and one of the items I want is in "latestloans" which is where all that data shows up. so I cant figure out how to grab one of the items
Let's assume you're trying to grab the forecast url. It's as simple as:
// update this line
NSDictionary *latestLoans = [json objectForKey:#"current_observation"];
// url variable will contain the first forecast url in the array
NSString *url = [latestLoans objectForKey:#"forecast_url"];

Resources