Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 解析json数据时未显示正确的值_Iphone_Objective C - Fatal编程技术网

Iphone 解析json数据时未显示正确的值

Iphone 解析json数据时未显示正确的值,iphone,objective-c,Iphone,Objective C,当输入NSLog(标题)位置和城市时,它显示零值 JSON: 按照Praveen的回答,我尝试了此代码,但仍然不起作用: [ { "date": 1311552000000, "events": [ { "affectedDate": 1311552000000, "event": { "appId": 42, "eventId": 18095, "location": "

当输入NSLog(标题)位置和城市时,它显示零值

JSON:

按照Praveen的回答,我尝试了此代码,但仍然不起作用:

[
  {
    "date": 1311552000000,
    "events": [
      {
        "affectedDate": 1311552000000,
        "event": {
          "appId": 42,
          "eventId": 18095,
          "location": "Sjølingstad Uldvarefabrik",
          "municipality": "Lindesnes",
          "title": "Utstillingsåpning - romfisk"
        }
      },
      {
        "affectedDate": 1311552000000,
        "event": {
          "appId": 42,
          "eventId": 18095,
          "location": "Sjølingstad Uldvarefabrik",
          "municipality": "Lindesnes",
          "title": "Utstillingsåpning - romfisk"
        }
      },
      {
        "affectedDate": 1311580800000,
        "event": {
          "appId": 620,
          "eventId": 19490,
          "location": "Høvåg Gjestehus, Vestre Vallesverd, Høvåg",
          "municipality": "Lillesand",
          "title": "Kunstutstilling på Høvåg Gjestehus"
        }
      },
...
] 
  • 您不需要初始化解析器,b/c SBJSON使用NSString类别向名为
    JSONValue
    的标准NSString添加额外的解析方法,该方法返回
    NSDictionary
    表示
    NSString

  • 获得NSDictionary后,将其转换为NSArray,如下所示
    NSArray*values=[dictionary allValues]
    然后使用方法
    [values objectForKey:@“date”]
    获取日期,依此类推。

    其嵌套的Json。值的解析和检索是不同的。一旦获得嵌套对象的字典值,就需要深入了解它以获得下一个值

  • 从事件的主数组getobject
  • 从上述字典中获取事件的对象
  • 从步骤2中返回的字典中,您可以访问appId、eventId等
  • 所以基本上你的代码应该是

    NSDictionary *dictOne = [results objectAtIndex:0];
    NSArray *activitiesArray = [dictOne objectForKey:@"events"];
    NSDictionary *dictTwo = [activitiesArray objectAtIndex:1];
    NSArray *eventArray=[dictTwo objectForKey:@"event"];
    NSDictionary *dictThree=[eventArray objectAtIndex:2];
    

    请提供json,以便我们了解其结构。这是我正在使用的json数据。请编辑您自己的问题并进行更新,而不是其他人回答。正如您在代码中看到的,我正在使用它的showign date affecteddate,但不显示其他值
    NSDictionary *dictOne = [results objectAtIndex:0];
    NSArray *activitiesArray = [dictOne objectForKey:@"events"];
    NSDictionary *dictTwo = [activitiesArray objectAtIndex:1];
    NSArray *eventArray=[dictTwo objectForKey:@"event"];
    NSDictionary *dictThree=[eventArray objectAtIndex:2];
    
    NSDictionary *dictTwo = [activitiesArray objectAtIndex:1]; // Or get object for key events