【问题标题】:Objective C parse JSONObjective C 解析 JSON
【发布时间】:2014-08-26 10:47:39
【问题描述】:

我有这个结果 JSON 下面是我从我的 wcf 服务获得的,我想在我的 ipad 应用程序中迭代它。我使用下面的代码来解析结果 json,但应用程序给出了错误。如何遍历这个字典并获取项目。

IOS代码:

   myArray = [NSMutableArray array];
        // Do any additional setup after loading the view.
        NSString* URL = @"http://www.unluapps.com/Service1.svc/ListTopReports";
        NSDictionary *dictionary = [JSONHelper loadJSONDataFromURL:URL ];
        NSString *result=[dictionary valueForKey:@"ListTopReportsResult"];
        NSLog(@"%@",result);

   /* for (NSString* key in result ) {

        id value=[result objectForKey:key];
        NSLog(@"%@ -+",value);

    }
   */

JSON:

{
ListTopReportsResult: [
{
AppUserID: null,
Author: "Joe Matthew",
Company: "Coca-Cola",
Country: "Poland",
CreationDate: "8/20/2014 4:32:00 AM",
EndDate: null,
ResearchReportID: "2",
Sector: "Soft Drinks",
Summary: "da ascon casmld lmasdlasd",
Title: "Can Coca-cola beat the market?",
URL: "2123123.pdf"
},
{
AppUserID: null,
Author: "martina pawlik",
Company: "Cimsa",
Country: "Poland",
CreationDate: "8/20/2014 4:31:00 AM",
EndDate: null,
ResearchReportID: "1",
Sector: "Cement",
Summary: "asd adas asd asdasd asdasdasd",
Title: "Poland's cement sector is on the rise",
URL: "1123123.pdf"
}
]
}

【问题讨论】:

  • result 应该是 NSArray,而不是 NSString
  • 这可以帮助你 NSArray *yourArray=[dictionary valueForKey:@"yourKey"];

标签: ios asp.net objective-c json wcf


【解决方案1】:

试试这个:

NSArray *result=[dictionary valueForKey:@"ListTopReportsResult"];

因为在您的 JSON 中,有一个键为 ListTopReportsResult 的数组。

希望这会有所帮助。 :)

【讨论】:

    【解决方案2】:

    尝试做这样的事情。这不是确切的解决方案,您必须按自己的方式解决。

    NSString *link =  [NSString stringWithFormat:@"yourServiceURL"];
        NSString *encdLink = [link stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        NSURL *url=[NSURL URLWithString:encdLink];
        NSData *response = [NSData dataWithContentsOfURL:url];
        NSError *error;
        NSDictionary *json = [NSJSONSerialization JSONObjectWithData:response options: NSJSONReadingMutableContainers error: &error];
    

    现在您将数据放入数组并使用objectForKeyvalueForKey 将其提取出来。您可以根据需要使用NSDictionaryNSArray

    首先从上面的代码中得到数组。然后像这样提取图像内容:

    _demoArray = [json valueForKey:@"yourKeyField"];
    

    现在您的demoArray 中有值。

    希望这会有所帮助。

    【讨论】:

    • 我尝试了 Rashad 的答案,它奏效了,我也会对此进行测试并发布结果,但由于 Stackoverflow 只接受一个答案,我会将其保存为有用的。感谢您的快速回复。
    猜你喜欢
    • 2013-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多