解析JSON。我有服务器响应

时间:2012-03-20 12:18:35

标签: iphone ios

  

可能重复:
  Json Passing in iphone

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

先谢谢

{

    Successfully =   

  (

                {

            0 =             {

              DeviceName = Tommy;

                DeviceTypeId = 1;

                EMEI = xxxxxx;

                GId = xxxxx;

                Id = 105;

                Pet = "<null>";

                PetImage = "352022008228784.jpg";

                ProtocolId = xxxx;

                SimNo = xxxxx;
            };

        }
    );
}

4 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

使用NSDictionary和方法valueForKey。

答案 2 :(得分:0)

假设这是完整的JSON响应,您将需要从该0索引创建一个类似于

的字典。
NSMutableDictionary *dictionary = [JSON objectForKey:@"0"];

其中JSON是存储它的对象的名称。然后,您应该能够通过获取所需的密钥对象来访问其中的所有内容。

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

这些都没有经过测试,而且只是从记忆中完成的,所以请稍等一些。

答案 3 :(得分:0)

This link可能会帮助您解决问题。