【问题标题】:How do I parse this nested JSON output in Objective C?如何在 Objective C 中解析这个嵌套的 JSON 输出?
【发布时间】:2011-08-30 08:22:51
【问题描述】:

我正在尝试在 Objective 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"
}
]
}

我有一些代码在工作,但我发现它的嵌套性质很难弄清楚。我想得到的是一个数组/字典,其中包含每个返回记录的以下元素:坐标、CountryName、ThoroughfareName、PostalCode 和 Accuracy。

这是我目前的测试代码:

- (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];
}

我的代码只是设法获取原始 JSON 输出,任何想法/代码来分解它,正如我上面提到的那样。

谢谢!

根据要求,提供更多信息。

  1. 使用 SBJson 框架。
  2. 上面的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;
    };
    

    }

【问题讨论】:

  • 在此处发布您的 nslog json 响应输出
  • 另外你使用什么框架来解析你的 JSON?您是否尝试过 JSONLint 来查看 JSON 是否有效?
  • 您可以从数组中的JSON数据中获取所有字典,然后使用valueForKey方法检索countryName、Thouroughfare name等......
  • @booleanBoy - 我遇到了您的建议,但不确定如何实施...请提供任何建议?
  • 首先,您是否从字典中的 JSON 中获取数据?

标签: iphone objective-c json parsing


【解决方案1】:

试试这个:

//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);

同样,您也可以获得“id”和“address”值的数组。

要获取点数组的“坐标”值,请执行此操作

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

对于地址详细信息/国家/国家/地区名称:

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

【讨论】:

  • @spaleja:数组addressDetailspoint 将在哪里发布?此代码泄漏内存。
  • 你可以释放addressDetails和点数组,一旦你会从字典中得到invidual数组。
【解决方案2】:

您的 json 无效,尝试在 http://jsonviewer.stack.hu/ 上测试它,一旦它变得有效,那么只有您可以使用它。您必须使用 SBJSON 框架来解析它。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-01
  • 2021-03-17
  • 2016-01-28
相关资源
最近更新 更多