如何在JSON中从Web检索的NSObject中检索字符串类型的值?

时间:2016-04-02 07:07:48

标签: objective-c json

我有一个NSObject boastObj,它包含string类型的购买属性。我正在通过JSON parse检索值,这是结果的子对象。

@interface BoasterElite : NSObject    
{
    NSString* purchase;
}

以下是JSON。

"results": [
    {
        "name": "UserName",
        "points": "2",
        "profile_photo": "https:\/\/scontent.fblr1-1.fna.fbcdn.net\/hphotos-xtf1\/v\/t1.0-9\/12004045_134589590222520_1895675040105383396_n.jpg?oh=17c11a393c141443bdd3bc1451a560a8",
        "purchase": [
            {
                "purchase_id": "1",
                "purchase_type": "car",
                "timeDate": "2016-04-01 00:10:49",
                "brand": "Ferrari",
                "model": "v3",
                "year": "2016",
                "photo": "https:\/\/cdn1.vox-cdn.com\/thumbor\/-pG8Dcb_qtRf6te3ug12FHhqUDs=\/1020x0\/cdn0.vox-cdn.com\/uploads\/chorus_asset\/file\/4156848\/Ferrari_F12tdf_3low.0.jpg",
                "message": "ferrari car in boaster app",
                "verify_id": "0"
            }
        ]
    }
],

2 个答案:

答案 0 :(得分:0)

将json值存储在NSDictionary或NSArray中。

如果要检索NSString值,请使用

NSString *name = [[json objectForKey:@"results"] objectForKey:@"name"];



NSString *purchase_type = [[[json objectForKey:@"results"] objectForKey:@"purchase"] objectForKey:@"purchase_type"];

它可能对你有帮助。

答案 1 :(得分:0)

results是一个字典数组,假设json是JSON对象

NSArray *results = json["results"];

您可以使用

获取points
NSString *points = results[0]["points"];

purchase位于数组的第一项,它也是一个字典数组。

NSArray *purchases = results[0]["purchase"];
NSDictionary *purchase = purchases[0];

现在您可以按键获取值,例如

NSString *brand = purchase["brand"];