处理JSON数据,Iphone与Android

时间:2011-10-04 19:14:33

标签: android iphone json

获取JSON记录JSON:

{
    content : [
        { action : "eat ice-cream", username : "tom" },
        { action : "play sport", username : "mike" },
        { action : "dancing", username : "dan" }
    ],
    message : "OK",
    status : 1
}

iPhone: Object将包含一个action和username字段。 在回调函数中

-(void) fetchdatacompleted:(NSDictionary*)dict
{
    if([dict objectForKey:@"content"] != nil)
    {
        [objectArray removeAllObjects];

        if([[dict objectForKey:@"content"] isKindOfClass:[NSArray class]])
        {
            NSArray *array = (NSArray *)[dict objectForKey:@"content"];

            for (NSDictionary *dict in array)
            {
                myobject = [[Object alloc] initWithDict:dict];
                [objectArray addObject:myobject];               
            }
        }
    }
}

Android如何处理上述处理?

1 个答案:

答案 0 :(得分:1)

查看JSONObjectJSONArray

示例:

JSONObject json = new JSONObject(jsonString);
JSONArray content = json.JSONArray("content");

for (int i = 0; i < content.length(); i++) {
    JSONObject entry = content.getJSONObject(i);
    // and so on...
}