如何从这个特定的json(iphone)中获取数据

时间:2011-12-19 08:14:55

标签: iphone json

有人能告诉我如何从中找到model吗?

它一直给我null

 {
"Status": 200,
"Data": [
    {
        "id": "1",
        "custno": "0000235",
        "locno": "00001",
        "mfg": "KABA-MAS",
        "model": "Cencon Gen II",
        "serial": "GF8912",
        "install": "2011-10-01",
        "removed": "0000-00-00",
        "warranty": "2012-01-10",
        "seragrno": "A",
        "equiploc": "Testing",
        "notes": "This is a test lock to test our system.  I this is it good.",
        "invoice": "A",
        "eqtype": "Lock",
        "plan": "1",
        "status": "0",
        "image": "cencon_main_1318703242.gif",
        "pmrequired": "1",
        "locktime": null,
        "lockby": null,
        "pmrequiredText": "Yes",
        "statusText": "Active",
        "planTypeText": "Plan I"
    },
    {
        "id": "2",
        "custno": "0000235",
        "locno": "00001",
        "mfg": "adsdad",
        "model": "",
        "serial": "",
        "install": "2011-10-24",
        "removed": "0000-00-00",
        "warranty": "0000-00-00",
        "seragrno": "",
        "equiploc": "",
        "notes": "",
        "invoice": "",
        "eqtype": "",
        "plan": "",
        "status": "-1",
        "image": "",
        "pmrequired": "0",
        "locktime": null,
        "lockby": null,
        "pmrequiredText": "No",
        "statusText": "Pending",
        "planTypeText": null
    }
]

}

4 个答案:

答案 0 :(得分:0)

我不确定......但是应该有一些obj.value类型的结构,比如在javascript中 你可以使用“responce.data.model”

之类的东西来访问它

答案 1 :(得分:0)

假设JSON已变为NSDictionary

NSArray *items = [dictionary objectForKey:@"Data"];
for (item in items) {
    NSLog(@"model = %@", [item objectForKey:@"model"]);
}

如果您正在寻找如何将JSON转换为NSDictionary尝试:

iPhone/iOS JSON parsing tutorial

答案 2 :(得分:0)

使用SBJsonParser解析响应。 “data”字段是字典数组。

SBJsonParser    *parser     = [[SBJsonParser alloc] init];

// parsing the JSON
NSMutableDictionary *jsonDictionary = [parser objectWithString:response];

NSMutableArray *dataArray = [jsonDictionary objectForKey:@"data"];

所以,你可以使用这样的东西: -

for(NSMutableDictionary *tempDictionary in dataArray)          
{
    NSLog(@"id is %@",[dataArray objectForKey: @"description"]);
}

答案 3 :(得分:0)

试试这个:

    NSData *responseData;
    NSMutableDictionary *response ;

    NSString *urlStr = [NSString stringWithFormat:@"http://www.example.com"];
    NSLog(@"%@",urlStr);
    NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:[urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]];

    responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

    NSString *json = [[[NSString alloc] initWithData:responseData encoding:NSASCIIStringEncoding] autorelease];

    response = (NSMutableDictionary *)[json JSONValue]; 

然后你会有字典response。现在您可以将其作为

进行访问
NSLog(@"%@",[response valueForKey:@"Data"] objectAtIndex:yourIndexValue] valueForKey:@"model"]);