解析JSON。我有服务器响应

问题描述:

可能重复:
Json Passing in iphone解析JSON。我有服务器响应

我的JSON解析是新。我有一个服务器响应如何获取0索引的“设备名称和ID”。

在此先感谢

{ 

    Successfully = 

    (

       { 

      0 =    { 

       DeviceName = Tommy; 

       DeviceTypeId = 1; 

       EMEI = xxxxxx; 

       GId = xxxxx; 

       Id = 105; 

       Pet = "<null>"; 

       PetImage = "352022008228784.jpg"; 

       ProtocolId = xxxx; 

       SimNo = xxxxx; 
      }; 

     } 
    ); 
} 
+0

我们在看什么?这是一些简单的字符串或数据收到或者这已经解析了吗? – 2012-03-20 12:34:53

+0

显然这是从一个NSLog – Alladinian 2012-03-20 12:38:02

NSDictionary *myJsonResponse; // Let's assume that this is the response you provided 

NSString *deviceName = [[[myJsonResponse valueForKey:@"Successfully"] objectAtIndex:0] valueForKeyPath:@"0.DeviceName"]; 

id theId = [[[myJsonResponse valueForKey:@"Successfully"] objectAtIndex:0] valueForKeyPath:@"0.Id"]; // I am using "id" type here since I don't know if this is an NSString or an NSNumber 
+0

谢谢你是对的我忘记了“成功”。再次感谢 – UserSOF 2012-03-20 12:51:02

+0

我有更多的一个索引,但当我使用.... NSDictionary * menu = [parsedJSON objectForKey:@“Successfully”]; NSLog(@“计数值是%d”,[菜单计数]); .....它打印1,但我想输出2.根据服务器响应 – UserSOF 2012-03-20 14:39:26

+0

是的,这是正常的。在“成功”数组里,你只有一个对象(一个字典),有多个键/值。试试这个:'NSDictionary * menu = [[parsedJSON objectForKey:@“Successfully”] objectAtIndex:0];'然后计算菜单对象。 – Alladinian 2012-03-20 14:45:36

使用的NSDictionary和方法valueForKey。

+0

我使用这个,但。 out put is null ...... NSDictionary * menu = [parsedJSON objectForKey:@“0”]; (@“PARESED JASON 0th index%@ VALUE”,[parsedJSON objectForKey:@“0”]); NSLog(@“菜单是%@”,菜单); – UserSOF 2012-03-20 12:26:30

+0

@GauravBOSS你忘记了“成功”数组,这也是你的字典中的一个关键。请参阅下面的答案。 – Alladinian 2012-03-20 12:40:30

假设这是完整的JSON响应,你需要创建从0指数,这应该是这个样子

NSMutableDictionary *dictionary = [JSON objectForKey:@"0"]; 

其中JSON是,这是存储在对象的名字的字典。然后,你应该能够内刚刚起步的对象关键要即

NSString *deviceName = [dictionary objectForKey:@"DeviceName"]; 

无本进行了测试,只是从内存中完成,从而采取与一粒盐访问一切。

This link可能会帮助你解答。