简体   繁体   English

如何在Objective C中解析此嵌套的JSON输出?

[英]How do I parse this nested JSON output in Objective C?

I'm trying to parse a nested JSON output like this in Objective C: 我正在尝试在目标C中解析这样的嵌套JSON输出:

{
-Status: {
code: 200
request: "geocode"
name: "1.304044,103.833867"
}
-Placemark: [
-{
-Point: {
-coordinates: [
103.834
1.30396
0
]
}
-AddressDetails: {
-Country: {
CountryName: "Singapore"
-Thoroughfare: {
ThoroughfareName: "Bus stop at Lucky Plaza (09048)"
}
CountryNameCode: "SG"
}
Accuracy: 9
}
id: "p1"
address: "Bus stop at Lucky Plaza (09048)"
}
-{
-Point: {
-coordinates: [
103.834
1.30444
0
]
}
-AddressDetails: {
-Country: {
CountryName: "Singapore"
AddressLine: "Lucky Plaza"
-Thoroughfare: {
-PostalCode: {
PostalCodeNumber: "238863"
}
ThoroughfareName: "304 Orchard Road"
}
CountryNameCode: "SG"
}
Accuracy: 9
}
id: "p2"
address: "Lucky Plaza, 304 Orchard Road, Singapore 238863"
}
-{
-Point: {
-coordinates: [
103.833
1.30376
0
]
}
-AddressDetails: {
-Country: {
CountryName: "Singapore"
AddressLine: "Wisma Atria"
-Thoroughfare: {
-PostalCode: {
PostalCodeNumber: "238877"
}
ThoroughfareName: "435 Orchard Road"
}
CountryNameCode: "SG"
}
Accuracy: 9
}
id: "p3"
address: "Wisma Atria, 435 Orchard Road, Singapore 238877"
}
-{
-Point: {
-coordinates: [
103.835
1.30389
0
]
}
-AddressDetails: {
-Country: {
CountryName: "Singapore"
-Thoroughfare: {
-PostalCode: {
PostalCodeNumber: "238860"
}
ThoroughfareName: "291 Orchard Road"
}
CountryNameCode: "SG"
}
Accuracy: 9
}
id: "p4"
address: "291 Orchard Road, Singapore 238860"
}
-{
-Point: {
-coordinates: [
103.834
1.30491
0
]
}
-AddressDetails: {
-Country: {
CountryName: "Singapore"
AddressLine: "Kimsia Park"
-Thoroughfare: {
-PostalCode: {
PostalCodeNumber: "228968"
}
ThoroughfareName: "1 Jalan Kayu Manis"
}
CountryNameCode: "SG"
}
Accuracy: 9
}
id: "p5"
address: "Kimsia Park, 1 Jalan Kayu Manis, Singapore 228968"
}
]
}

I have some code working, but I'm finding the nested nature of this to be pretty tough to figure out. 我有一些代码在工作,但是我发现它的嵌套性质很难弄清楚。 What I'd like to get out is an array/dictionary which contains the following elements for each return record: coordinates, CountryName, ThoroughfareName, PostalCode and Accuracy. 我想要得到的是一个数组/字典,其中包含每个返回记录的以下元素:坐标,CountryName,ThoroughfareName,PostalCode和Accuracy。

Here is my test code so far: 到目前为止,这是我的测试代码:

- (void)viewDidLoad {
    [super viewDidLoad];

    NSURL *jsonURL = [NSURL URLWithString:@"http://gothere.sg/maps/geo?output=json&ll=1.304044%2C103.833867&client=&sensor=false&callback="];

    NSString *jsonData = [[NSString alloc] initWithContentsOfURL:jsonURL];

    self.jsonArray = [jsonData JSONValue]; 

    NSLog(@"%@", jsonArray);

    [jsonURL release];
    [jsonData release];
}

My code just manages to get the raw JSON output, any thoughts/code to break it down as I've mentioned above. 我的代码只是设法获取原始的JSON输出,正如我上面提到的,任何将其分解的想法/代码。

Thanks! 谢谢!

As requested, some further information. 根据要求,一些进一步的信息。

  1. Using SBJson framework. 使用SBJson框架。
  2. NSLog output from above: 上面的NSLog输出:

     2011-08-30 16:44:32.605 Taxi[53070:207] { Placemark = ( { AddressDetails = { Accuracy = 9; Country = { CountryName = Singapore; CountryNameCode = SG; Thoroughfare = { ThoroughfareName = "Bus stop at Lucky Plaza (09048)"; }; }; }; Point = { coordinates = ( "103.834", "1.30396", 0 ); }; address = "Bus stop at Lucky Plaza (09048)"; id = p1; }, { AddressDetails = { Accuracy = 9; Country = { AddressLine = "Lucky Plaza"; CountryName = Singapore; CountryNameCode = SG; Thoroughfare = { PostalCode = { PostalCodeNumber = 238863; }; ThoroughfareName = "304 Orchard Road"; }; }; }; Point = { coordinates = ( "103.834", "1.30444", 0 ); }; address = "Lucky Plaza, 304 Orchard Road, Singapore 238863"; id = p2; }, { AddressDetails = { Accuracy = 9; Country = { AddressLine = "Wisma Atria"; CountryName = Singapore; CountryNameCode = SG; Thoroughfare = { PostalCode = { PostalCodeNumber = 238877; }; ThoroughfareName = "435 Orchard Road"; }; }; }; Point = { coordinates = ( "103.833", "1.30376", 0 ); }; address = "Wisma Atria, 435 Orchard Road, Singapore 238877"; id = p3; }, { AddressDetails = { Accuracy = 9; Country = { CountryName = Singapore; CountryNameCode = SG; Thoroughfare = { PostalCode = { PostalCodeNumber = 238860; }; ThoroughfareName = "291 Orchard Road"; }; }; }; Point = { coordinates = ( "103.83499999999999", "1.30389", 0 ); }; address = "291 Orchard Road, Singapore 238860"; id = p4; }, { AddressDetails = { Accuracy = 9; Country = { AddressLine = "Kimsia Park"; CountryName = Singapore; CountryNameCode = SG; Thoroughfare = { PostalCode = { PostalCodeNumber = 228968; }; ThoroughfareName = "1 Jalan Kayu Manis"; }; }; }; Point = { coordinates = ( "103.834", "1.30491", 0 ); }; address = "Kimsia Park, 1 Jalan Kayu Manis, Singapore 228968"; id = p5; } ); Status = { code = 200; name = "1.304044,103.833867"; request = geocode; }; 

    } }

Try this : 尝试这个 :

//NSMutableArray *addressDetails = [[NSMutableArray alloc] init];
NSMutableArray *addressDetails = [[jsonArray valueForKey:@"Placemark"] valueForKey:@"AddressDetails"];

  //NSMutableArray *point = [[NSMutableArray alloc] init];
NSMutableArray *point = [[jsonArray valueForKey:@"Placemark"] valueForKey:@"Point"];

NSLog(@"%@", point);
NSLog(@"%@", [point objectAtIndex:0]);

NSLog(@"%@", addressDetails);

Like wise you can get array of "id" and "address" value also. 同样,您也可以获取“ id”和“ address”值的数组。

To get value of "coordinates" of point array, do this 要获取点数组的“坐标”值,请执行此操作

NSLog(@"%@", [[point objectAtIndex:0] valueForKey:@"coordinates"]);

For AddressDetails/Country/CountryName : 对于AddressDetails / Country / CountryName:

NSLog(@"%@", [[[addressDetails objectAtIndex:0] valueForKey:@"Country"] valueForKey:@"CountryName"]);

Your json is invalid, try to test it on http://jsonviewer.stack.hu/ once it becames valid then only you can use it. 您的json无效,请尝试在http://jsonviewer.stack.hu/上对其进行测试,一旦它变为有效,则只有您可以使用它。 You must have used the SBJSON framework to parse it. 您必须已经使用SBJSON框架对其进行了解析。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM