Reputation: 63
Can anyone help me parse this complex nested json data?
I am working with iPhone Xcode 4.3 and objective-c.please provide small demo.
{"html_attributions" : [],
"results" : [
{
"geometry" : {
"location" : {
"lat" : 61.1820430,
"lng" : -149.8003850
}
},
"icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png",
"id" : "207c77fb1258ef8cb22abe391c4924850a1ac045",
"name" : "Alaska Native Medical Center Rad",
"reference" : "CoQBdwAAAG5xNU-hb4BzNd-9H8IcLHcnz8txVe2j00HjRmjInS6uYt0j4qnAhOU_FNkHSSjvZd47dVu8Vk8Vj2iuu0NDDDa3tkCK-yHbUaj1Wrg5DNFvPmou1jvsjUE6gRKUAHXXemRJTNgtDfvhnS5kNdS0c71r9Df2S_Yvu0m92z62D0TZEhACm7dSGWKcpfZ_H-zTi5BzGhRCM22q0rCD6vjD3keZfBELfn_VsA",
"types" : [ "hospital", "health", "establishment" ],
"vicinity" : "4315 Diplomacy Drive, Anchorage"
}
],
"status" : "OK"
}
Upvotes: 1
Views: 832
Reputation: 441
For this kind of complex JSON string you can not use google's JSON frame work.But to assign that string to NSDictionary you will require that frame work. So first add that framework to your project.Then Add JSON.h file to your .m file. And then use assign that string as follows: NSDictionary *dic = [jsonString JSONValues]; After this line you will get all the values of your string in three keys. First key is "html_attributions" second key is "result" and third key is "status". For accessing the inner values of your second key you have to assign that to another NSDictionary or NSArray variable.And use key to access the values.
It will work for only above mentioned JSON String.
Upvotes: 1
Reputation: 1247
If your on the iPhone you can use various JSON Libraries to turn JSON strings into NSDictionary and NSArray objects
JSON Kit is a very fast and lightweight library that you can use: https://github.com/johnezang/JSONKit
Or there is JSON-framework: http://code.google.com/p/json-framework/
Also if targeting iOS 5 devices, Apple has included their own JSON deserializer. Take a look at the NSJSONSerialization class
Upvotes: 0
Reputation: 12710
Why don't you use a framework for that job?
You can try json-framework (formerly SBJSon).
Project page : https://github.com/stig/json-framework
Upvotes: 0
Reputation: 108
There's an objective-c library called JSONKit that works pretty well. It'll give you an NSArray or NSDictionary that you can then get your data out of.
https://github.com/johnezang/JSONKit
Its as easy as doing this:
NSDictionary *unserializedData = [jsonString objectFromJSONString];
where jsonString is your json data from above that you want to parse in an NSString.
Upvotes: 1