我认为这对于尝试解析的人来说是一个非常普遍的问题,但我仍然对我需要做的事情感到茫然:
我有一个json,我正在从php webservice响应中读取它:
{
Name: "Balrog",
Origin: "USA",
Age: "28",
Height: "6`5",
Weight: "252 lbs",
Game: "Street Fighter",
Image: "N/A"
},
{
Name: "Faust",
Origin: "Parts Unknown",
Age: "Unknown",
Height: "9`1",
Weight: "165 lbs",
Game: "Guilty Gear",
Image: "N/A"
},
{
Name: "Hugo",
Origin: "Germany",
Age: "28",
Height: "8`2",
Weight: "440 lbs",
Game: "Final Fight",
Image: "N/A"
}
但是当我试图解析时,我在宣布" NSDictionary"区域。我知道*结果' " objectforkey"需要改变,但我不知道。
listOfFighters = nil;
NSString* WebServiceURL = @"URL_GOES_HERE";
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue, ^{
// Get the JSON string from our web serivce
NSDictionary * dictionary = [JSONHelper loadJSONDataFromURL:WebServiceURL];
dispatch_async(dispatch_get_main_queue(), ^{
// Get a list of results in the Dictionary
NSArray *results = [dictionary objectForKey:@"GetAllFightersResult"];
答案 0 :(得分:2)
如果你发布的JSON是正确的,那么试试这个:
// Get the JSON string from our web service
NSArray * dictionary = [JSONHelper loadJSONDataFromURL:WebServiceURL];
每个索引都包含一个NSDictionary。希望这有助于.. :))
答案 1 :(得分:0)
给定的JSON无效。请阅读http://www.json.org/上的文档,然后尝试通过http://jsonlint.com/验证您的JSON。
更正JSON示例:
[
{
"Name": "Balrog",
"Origin": "USA"
},
{
"Name": "Faust",
"Origin": "Parts Unknown"
}
]
这将为您提供一个带有NSDictionaries的NSArray,其中包含Key-Value-Pair“Name”和“Origin”。