如何从Objective C,iPhone中的这个JSON字符串中获取值

时间:2014-01-31 19:10:40

标签: iphone objective-c

我想解析一个看起来像这样的JSON字符串。

Json String中有更多代码,我在这里粘贴一个示例。

[

{  "Mainhd":"Select The Correct Adjective From The Given Options.",


  "Sub":

    [
        {
            "quetn":"Bill is two years ___ than Wanda.",
            "Answr":"1",  
            "optns":"1,2",                   
                "1":"younger",
                "2":"smaller"
        },
        {
            "quetn":"France is ___ European country.",
            "Answr":"2",
            "optns":"1,2",                   
                "1":"an",
                "2":"a"
        }]
}
]

[ { "Mainhd":"Select The Correct Adjective From The Given Options.", "Sub": [ { "quetn":"Bill is two years ___ than Wanda.", "Answr":"1", "optns":"1,2", "1":"younger", "2":"smaller" }, { "quetn":"France is ___ European country.", "Answr":"2", "optns":"1,2", "1":"an", "2":"a" }] } ]

对于解析上面的JSON,我在Objective C代码中这样做。

NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
    NSError* error;

    NSDictionary* json = [NSJSONSerialization
                          JSONObjectWithData: jsonData //1
                          options:kNilOptions
                          error:&error];

    //NSLog(@"JSON : %@",json);
    if(error){
        NSLog(@"Error %@", [error localizedDescription]);
    }

    NSString *latestLoans = [json valueForKey:@"Mainhd"];
    NSLog(@"Mainhd: %@", latestLoans); //3

    NSString *sub = [json valueForKey:@"Sub"];
    NSLog(@"SUB: %@", sub); //3

执行此代码时,我收到此输出。

NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding]; NSError* error; NSDictionary* json = [NSJSONSerialization JSONObjectWithData: jsonData //1 options:kNilOptions error:&error]; //NSLog(@"JSON : %@",json); if(error){ NSLog(@"Error %@", [error localizedDescription]); } NSString *latestLoans = [json valueForKey:@"Mainhd"]; NSLog(@"Mainhd: %@", latestLoans); //3 NSString *sub = [json valueForKey:@"Sub"]; NSLog(@"SUB: %@", sub); //3

之后我想从“SUB”中获取值。

我正在尝试这样,但没有工作

SUB: (
        (

{
            1 = younger;
            2 = smaller;
            Answr = 1;
            optns = "1,2";
            quetn = "Bill is two years ___ than Wanda.";
        },
                {
            1 = an;
            2 = a;
            Answr = 2;
            optns = "1,2";
            quetn = "France is ___ European country.";
        },
                {
            1 = gorgeous;
            2 = gorgeousest;
            3 = gorgeouser;
            Answr = 1;
            optns = "1,2,3";
            quetn = "Arya is looking _______ in this dress.";
        }

) )

SUB: ( ( { 1 = younger; 2 = smaller; Answr = 1; optns = "1,2"; quetn = "Bill is two years ___ than Wanda."; }, { 1 = an; 2 = a; Answr = 2; optns = "1,2"; quetn = "France is ___ European country."; }, { 1 = gorgeous; 2 = gorgeousest; 3 = gorgeouser; Answr = 1; optns = "1,2,3"; quetn = "Arya is looking _______ in this dress."; }

我收到了NULL。

1 个答案:

答案 0 :(得分:0)

值得注意的是,在您发布的JSON中,sub的值是一个数组,因此您需要将其视为此类并循环遍历它以获取您的值

    [

{  "Mainhd":"Select The Correct Adjective From The Given Options.",


  "Sub":

    [
        {
            "quetn":"Bill is two years ___ than Wanda.",
            "Answr":"1",  
            "optns":"1,2",                   
                "1":"younger",
                "2":"smaller"
        },
        {
            "quetn":"France is ___ European country.",
            "Answr":"2",
            "optns":"1,2",                   
                "1":"an",
                "2":"a"
        }]
}
]

因此,以下内容应指向正确的方向

          NSArray *subArray = [json valueForKey:@"Sub"];

        //loop array using each response as a dictionary

        for (NSDictionary *key in subArray){

//log the value to console

        NSLog (@"Value is %@", [key objectForKey:@"quetn");

    }

希望有所帮助