在iOS中使用NSJSONSerialization进行JSON解析 [英] JSON parsing using NSJSONSerialization in iOS

查看:161
本文介绍了在iOS中使用NSJSONSerialization进行JSON解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的代码中解析 JSON 。但是在检索解析的 JSON 的数据时,我遇到了一些意想不到的问题。所以让我解释一下我的问题。

I am parsing a JSON in my code. But I am getting some unexpected issues while retrieving data of parsed JSON. So let me explain my problem.

我必须使用xcode解析 JSON 数据。当我在浏览器中点击相同的URL时,这就是要解析的数据:

I have to parse following JSON data using xcode. This is what data to be parsed looks like while I hit same URL in browser:

{
"RESPONSE":[
    {"id":"20",
    "username":"john",
    "email":"abc@gmail.com",
    "phone":"1234567890",
    "location":"31.000,71.000"}],
"STATUS":"OK",
"MESSAGE":"Here will be message"
}

我的代码达到此 JSON 数据如下:

My code to reach up to this JSON data is as follow:

NSData *data = [NSData dataWithContentsOfURL:finalurl];
NSError *error;
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];

如果我使用<$打印对象 json c $ c> NSLog ,即 NSLog(@json:%@,[json description]); 它看起来像:

If I print object json using NSLog, i.e. NSLog(@"json: %@", [json description]); it looks like:

json: {
    MESSAGE = "Here will be message";
    RESPONSE =(
                {
            email = "abc@gmail.com";
            id = 20;
            location = "31.000,71.000";
            phone = 1234567890;
            username = john;
        }
    );
    STATUS = OK;
}

所以,在这里我发现了两件事:

So, here I observed two things:


  1. 密钥(或节点)的顺序( MESSAGE RESPONSE STATUS )已更改。

RESPONSE 包含在'('&')'大括号中。

现在,如果我将键的值分开 MESSAGE & ; STATUS NsLog ,然后将它们打印为正确的字符串。

Now if I separate out values for keys MESSAGE & STATUS and NsLog them, then they get printed as proper string.

喜欢 msgStr:这里有消息& 状态:确定

如果我将 RESPONSE 作为字典并再次分离该字典的子值,如电子邮件用户名 ,然后我无法将其作为字符串

But if I separate out value for RESPONSE as a dictionary and again separating sub values of that dictionary like email and username, then I can't getting those as string.

以下是我写的代码:

NSMutableDictionary *response = [json valueForKey:@"RESPONSE"];
NSString *username = [response valueForKey:@"username"];
NSString *emailId = [response valueForKey:@"email"];

如果我打印用户名 emailId ,然后他们没有打印成普通字符串,而是输出为:

If I print username and emailId, then they are not getting printed as normal string, instead it outputs as:

username:(
    john
)

email:(
    abc@gmail.com
)

所以我的问题是为什么它不是正常的字符串?如果我尝试进一步使用这些变量,那么它们会显示在'('&')'括号内的值。这是因为 NSJSONSerialization 吗?

So my question is why it's not getting as a normal string? If I tried to use this variables further then they show value enclosed within '(' & ')' braces. Is this happening because of NSJSONSerialization?

推荐答案

首先在你的 JSON 回复词典,在键' RESPONSE '下你有一个数组而不是字典,该数组包含字典对象。
所以要提取用户名电子邮件ID ,如下所示

First of all in your JSON response dictionary, under the key 'RESPONSE' you have a array not a dictionary and that array contains dictionary object. So to extract username and email ID so as below

NSMutableDictionary *response = [[[json valueForKey:@"RESPONSE"] objectAtIndex:0]mutableCopy];
 NSString *username = [response valueForKey:@"username"];
 NSString *emailId = [response valueForKey:@"email"];

这篇关于在iOS中使用NSJSONSerialization进行JSON解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆