将JSON数据存储为字符串目标C.

时间:2022-01-11 16:56:47

So i have gotten my Json data in which i have got:

所以我得到了我的Json数据:

{
"name": "brad"
}

I am trying to store that individual name as a string.

我试图将该个人名称存储为字符串。

@try
{
     NSError *error;
NSString *url_string = [NSString stringWithFormat: @"http://Share/scripts/newjson.json"];
///Dummy URL
NSData *data = [NSData dataWithContentsOfURL: [NSURL URLWithString:url_string]];
NSMutableArray *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
NSLog(@"json: %@", json);

}
@catch (NSException *e)
{
    NSLog(@"Internet not enabled");
    //something went wrong
    //populate the NSError object so it can be accessed
}

But when i print out the json file i just get what is shown above, do i need to use a delimiter or something to get this string ?

但是,当我打印出json文件时,我得到上面显示的内容,我是否需要使用分隔符或其他东西来获取此字符串?

Thanks

谢谢

1 个解决方案

#1


2  

Actually the JSON is a dictionary not an array and with nilOptions not mutable.

实际上JSON是一个字典而不是一个数组,并且nilOptions不可变。

To get brad write

得到布拉德写

NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
NSString *name = json[@"name"];
NSLog(@"name: %@", name);

#1


2  

Actually the JSON is a dictionary not an array and with nilOptions not mutable.

实际上JSON是一个字典而不是一个数组,并且nilOptions不可变。

To get brad write

得到布拉德写

NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
NSString *name = json[@"name"];
NSLog(@"name: %@", name);