解析JSON字典/数组

发布于 2024-11-01 02:19:55 字数 691 浏览 1 评论 0原文

如果 JSON Web 服务返回类似这样的内容(房屋的详细信息),

[
    {
        id:4,
        price: 471,
        location: "New York",
        size: 3000
    },
    {
        id:7,
        price: 432,
        location: "London",
        size: 3200
    },
    {
        id:22,
        price: 528,
        location: "Tokyo",
        size: 2000
    }
]

我将如何一一迭代每个房屋?我正在使用 ASIHTTPRequest 和 JSON 解析器: http://stig.github.com/json-framework/

我想我可以像这样获取字典:

NSString *theResponse = [request responseString];
NSDictionary *dictionary = [theResponse JSONValue];

..但我不确定如何迭代每个房子。

If a JSON web service returns something like this (details of houses)

[
    {
        id:4,
        price: 471,
        location: "New York",
        size: 3000
    },
    {
        id:7,
        price: 432,
        location: "London",
        size: 3200
    },
    {
        id:22,
        price: 528,
        location: "Tokyo",
        size: 2000
    }
]

How would I iterate thru each house one-by-one? I'm using ASIHTTPRequest and the JSON parser: http://stig.github.com/json-framework/

I think I can get the dictionary like so:

NSString *theResponse = [request responseString];
NSDictionary *dictionary = [theResponse JSONValue];

.. but I'm unsure how to iterate thru each house.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

老旧海报 2024-11-08 02:19:55
{
        id:4,
        price: 471,
        location: "New York",
        size: 3000
    },
    {
        id:7,
        price: 432,
        location: "London",
        size: 3200
    },
    {
        id:22,
        price: 528,
        location: "Tokyo",
        size: 2000
    }

这是字典数组...您可以创建您的 House 的 Modal 类(属性:id,价格,位置,大小)并按如下方式迭代它...(考虑到您最终拥有上述内容)..

NSArray *houses = [dictionary objectForKey:<youHaveNotProvideItInYourData>];
NSMutableArray *populatedHouseArray = [[NSMutableArray alloc]init];
for(int i=0;i<[houses count];i++)
{
  NSDictionary *tempDictionary = [houses objectAtIndex:i];
  House *tempHouse = [[House alloc]init];
  if([tempDictionary objectForKey:@"id"]!=nil
  {
    tempHouse.id = [tempDictionary objectForKey:@"id"];
  }
  //and so on for other keys

  [populatedHouseArray addObject:tempHouse]; 
  [tempHouse release];
}

谢谢,

{
        id:4,
        price: 471,
        location: "New York",
        size: 3000
    },
    {
        id:7,
        price: 432,
        location: "London",
        size: 3200
    },
    {
        id:22,
        price: 528,
        location: "Tokyo",
        size: 2000
    }

This is array of dictionaries... you can create a Modal class of you House(attributed :id,price,location,size) and iterate it as follows...(considering you finally have above thing)..

NSArray *houses = [dictionary objectForKey:<youHaveNotProvideItInYourData>];
NSMutableArray *populatedHouseArray = [[NSMutableArray alloc]init];
for(int i=0;i<[houses count];i++)
{
  NSDictionary *tempDictionary = [houses objectAtIndex:i];
  House *tempHouse = [[House alloc]init];
  if([tempDictionary objectForKey:@"id"]!=nil
  {
    tempHouse.id = [tempDictionary objectForKey:@"id"];
  }
  //and so on for other keys

  [populatedHouseArray addObject:tempHouse]; 
  [tempHouse release];
}

Thanks,

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文