将JSON字符串解析为NSMutableArray [英] Parsing JSON string to a NSMutableArray

查看:291
本文介绍了将JSON字符串解析为NSMutableArray的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的字符串:

[{"id":"1","nome":"Adriatik"},{"id":"2","nome":"Ard"},{"id":"3","nome":"Albana"},{"id":"4","nome":"Adriana"}]

我想将JSON字符串的所有名称"解析为NSMutableArray.

I would like to parse all 'name' of the JSON string into a NSMutableArray.

对不起,我的英语!

推荐答案

每当我必须处理一些JSON代码时,我要做的第一件事就是基于JSON文本创建一个类.因此,例如,如果您的JSON表示美国州,请创建一个州"类.

Whenever I have to handle some JSON code, the first thing I like to do is create a class based on the JSON text. So, for example if your JSON is representing a U.S. state, create a "State" class.

有一个很酷的小产品可以用于此目的.它被称为 Objectify ,价格约为15美元.毫无疑问,人们可以建议其他可能做类似事情的免费物品.

There's a cool little product that you can use for this. It's called Objectify and costs about $15. No doubt people can advise on other free stuff that might do something similar.

对于实际的Json解析,我使用 SBJson .有很多针对Objective-C的Json解析框架,因此绝对可以环顾四周,看看有什么需要的.

For the actual Json parsing, I use SBJson. There's quite a few Json parsing frameworks out there for Objective-C so definitely have a look around to see what takes your fancy.

接下来,使用SBJson,进行实际的解析:

Next, with SBJson, do the actual parsing:

-(NSDictionary *)parseJsonFromUrl
{
    NSAssert(mUrl, @"Must set a url before invoking %@", __PRETTY_FUNCTION__);

    // Create new SBJSON parser object
    SBJsonParser *parser = [[SBJsonParser alloc] init];

    // Prepare URL request to download JSON
    NSURLRequest *request = [NSURLRequest requestWithURL:mUrl];

    // Perform request and get JSON back as a NSData object
    NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];

    // Get JSON as a NSString from NSData response
    NSString *json_string = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];

    // parse the JSON response into an object
    // Here we're using NSArray since we're parsing an array of JSON status objects
    return [parser objectWithString:json_string error:nil];
}

返回NSDictionary.您知道必须浏览该词典才能设置模型类的值.在将值同时加载到NSMutableArray

That returns a NSDictionary. You know have to look through that dictionary to set the values of your model class. Here's how to do that whilst at the same time loading the values into the NSMutableArray:

-(void)downloadJsonData
{    
    NSDictionary *statesDict = [self parseJsonFromUrl];

    NSMutableArray *statesArray = [NSMutableArray arrayWithCapacity:[statesDict count]];

    for (NSDictionary *stateDict in stateDict)
    {
        State *aState = [[[State alloc] init] autorelease];
        aState.stateId = [stateDict valueForKey:@"id"];
        aState.name = [stateDict valueForKey:@"name"];

        [statesArray addObject:aState];
    }
}

请注意,我使用的属性名称是stateId而不是id,以免与Objective-C对象指针类型发生冲突.

Note that I use a property name of stateId not id so as not to clash with the Objective-C object pointer type.

这篇关于将JSON字符串解析为NSMutableArray的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆