2015-07-20 77 views
0

我只需要知道解析JSON字符串的正确方法。 这里是我的示例JSON响应:解析JSON对象和子元素

[ 
    { 
     "my_response": [ 
      { 
       "name": "XXX", 
       "Area": "XXX", 
       "Num": 123 
      } 
     ] 
    }, 
    { 
     "other_response": [ 
      { 
       "message": "Hello", 
       "status": "Success", 
       "flag_value": "1" 
      } 
     ] 
    } 
] 

我想在String

我试过这个方法来解析flag_value

NSString *str1 = [json valueForKeyPath:@"other_response. flag_value"]; 
NSLog(@"str %@",str1); 

而且我的输出是一些什么样的这个

str (
    "<null>", 
    (
     1 
    ) 
) 

但我想我的输出是一个字符串像这样:

1 
+0

你的JSON响应是一个'NSString'还是你已经将它序列化为一个'NSArray/NSDictionary'对象? – timominous

+0

Nsdictionary * json –

+0

您的json没有'@“other_response。flag_value”'键 – Desdenova

回答

1
[{"my_response":[{"name":"XXX","Area":"XXX","Num":123}]},{"other_response":[{"message":"Hello","status":"Success","flag_value":"1"}]}] 

其实你的Json response开始于Array所以按照这个步骤

第1步

NSArray *jsonDict = [NSJSONSerialization JSONObjectWithData:yourData options:Kniloptions error:nil]; 

步骤2

在这里

你得到2个字典

NSString *FlagStr; 
for (NSMutableDictionary *temp in jsonDict) 
{ 

    NSArray *secondOption=[temp objectForKey:@"other_response"]; 

     for (NSMutableDictionary *second in secondOption) 
     { 
      FlagStr=[second objectForKey:@"flag_value"]; 
     } 

     } 

选择NO-2

我没有尝试这一点,但可能是它为你工作,一经查实

第1步

NSArray *jsonDict = [[[NSJSONSerialization JSONObjectWithData:yourData options:Kniloptions error:nil]objectAtIndex:1] objectForKey:@"other_response"]; 

步骤2

NSString *FlagStr; 

    for (NSMutableDictionary *second in secondOption) 
     { 
      FlagStr=[temp objectForKey:@"flag_value"]; 
     } 

选择NO-3

你可以直接读取字符串值我不是牛逼尝试这一点,但可能是它为你工作,一经查实

NSString *flage = [[[NSJSONSerialization JSONObjectWithData:yourData options:Kniloptions error:nil]objectAtIndex:1] objectForKey:@"other_response"]objectAtIndex:0] objectForKey:@"flag_value"]; 
+0

thanx修改其工作的 –

0

首先,我认为你的JSON将得到更好的格式如下所示:

{ 
    "my_response": { 
    "name": "XXX", 
    "area": "XXX", 
    "num": "XXX" 
    }, 
    "other_response": { 
    "message": "Hello", 
    "status": "success", 
    "flag_value": "1" 
    } 
} 

然后你可以用下面的代码来访问您的数据:

NSString *jsonString = @"{\"my_response\": {\"name\": \"XXX\",\"area\": \"XXX\",\"num\": \"XXX\"},\"other_response\": {\"message\": \"Hello\",\"status\": \"success\",\"flag_value\": \"1\"}}"; 
NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding]; 
NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableLeaves error:nil]; 
NSLog(@"str: %@", [jsonDict valueForKeyPath:@"other_response.flag_value"]); 
0
Format your Json array like this way. 

{ 
    "my_response": {"name": "XXX","area": "XXX","num": "XXX" 
    }, 
    "other_response": {"message": "Hello","status": "success","flag_value": "1" 
    } 
} 

**Step : 2** 

使用AFNetworking用于HTTP客户端

- (void)yourMethod{ 
    NSString *urlString = [NSString stringWithFormat:@"%@", your_service_url]; 
    NSURL *url = [NSURL URLWithString:urlString]; 

    AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url]; 
    [AFJSONRequestOperation addAcceptableContentTypes:[NSSet setWithObject:@"text/html"]]; 

    NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys: 

          your_parameters_list, 
          nil]; 
     NSMutableURLRequest *jsonRequest = [httpClient requestWithMethod:@"POST" 
                   path:urlString 
                  parameters:params]; 

    AFJSONRequestOperation *operation = 
    [AFJSONRequestOperation JSONRequestOperationWithRequest:jsonRequest success: ^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) { 
     NSLog(@" Success %@", JSON); 

     NSDictionary *jsonDictionary1 = [JSON valueForKey:@"my_response"]; 
     NSDictionary *jsonDictionary2 = [JSON valueForKey:@"other_response"]; 

       NSString* name = [jsonDictionary1 valueForKey:@“name”]; 
       NSString* area = [jsonDictionary1 valueForKey:@"name"]; 
       NSString* num = [jsonDictionary1 valueForKey:@"num"]; 




    } failure: ^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) { 
     NSLog(@"Fail %@", [error userInfo]); 

     NSLog(@“Error %@", [error localizedRecoverySuggestion]); 


    }]; 

    [operation start]; 
} 
0

如上哥哥Anbu.Karthick答案接受。但我想给这个答案

NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData:yourResponseData options: NSJSONReadingMutableContainers error: &err]; 

    for (int i =0; i<[jsonArray count]; i++) 

    { 

    NSMutableDictionary *dict = [[jsonArray objectAtIndex:i] mutableCopy]; 

    NSString *strFlag = [NSString stringWithFormat:@"%@",[[[dict objectForKey:@"other_response"] objectAtIndex:0] valueForKey:@"flag_value"]]; 

    NSLog(@"The strFlag is-%@",strFlag); 

    } 
+0

而不是NSString * strFlag = [NSString stringWithFormat:@“%@”,[[[dict objectForKey:@ “other_response”] objectAtIndex:0] valueForKey:@“flag_value”]]; –

+0

试试这个逻辑NSString * strFlag = [[[objectTypeForKey:@“other_response”] valueForKey:@“flag_value”] objectAtIndex:i]; –

+0

这是行不通的 – user3182143