Log*_*man 154 json objective-c nsdictionary ios
我有一个JSON字符串(来自PHP的json_encode()看起来像这样:
[{"id": "1", "name":"Aaa"}, {"id": "2", "name":"Bbb"}]
Run Code Online (Sandbox Code Playgroud)
我想将其解析为我的iPhone应用程序的某种数据结构.我想对我来说最好的事情就是拥有一个字典数组,所以数组中的第0个元素是一个带键"id" => "1"和字典的字典"name" => "Aaa".
我不明白如何NSJSONSerialization存储数据.到目前为止,这是我的代码:
NSError *e = nil;
NSDictionary *JSON = [NSJSONSerialization
JSONObjectWithData: data
options: NSJSONReadingMutableContainers
error: &e];
Run Code Online (Sandbox Code Playgroud)
这只是我在另一个网站上看到的一个例子.我一直试图JSON通过打印出那些元素和类似的东西来读取对象,但我总是得到EXC_BAD_ACCESS.
我如何使用NSJSONSerialization解析上面的JSON,并将其转换为我提到的数据结构?
rck*_*nes 209
您的根json对象不是字典而是数组:
[{"id": "1", "name":"Aaa"}, {"id": "2", "name":"Bbb"}]
Run Code Online (Sandbox Code Playgroud)
这可能会让您清楚地了解如何处理它:
NSError *e = nil;
NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData: data options: NSJSONReadingMutableContainers error: &e];
if (!jsonArray) {
NSLog(@"Error parsing JSON: %@", e);
} else {
for(NSDictionary *item in jsonArray) {
NSLog(@"Item: %@", item);
}
}
Run Code Online (Sandbox Code Playgroud)
sri*_*rik 73
这是我的代码,用于检查收到的json是否是数组或字典:
NSError *jsonError = nil;
id jsonObject = [NSJSONSerialization JSONObjectWithData:jsonData options:kNilOptions error:&jsonError];
if ([jsonObject isKindOfClass:[NSArray class]]) {
NSLog(@"its an array!");
NSArray *jsonArray = (NSArray *)jsonObject;
NSLog(@"jsonArray - %@",jsonArray);
}
else {
NSLog(@"its probably a dictionary");
NSDictionary *jsonDictionary = (NSDictionary *)jsonObject;
NSLog(@"jsonDictionary - %@",jsonDictionary);
}
Run Code Online (Sandbox Code Playgroud)
我已经尝试过这个选项:kNilOptions和NSJSONReadingMutableContainers,并且两者都能正常工作.
显然,实际的代码不能用于在if-else块中创建NSArray或NSDictionary指针的方式.
Ole*_*ann 28
这个对我有用.您的data对象可能是,nil并且正如rckoenes所指出的,根对象应该是(可变的)数组.看到这段代码:
NSString *jsonString = @"[{\"id\": \"1\", \"name\":\"Aaa\"}, {\"id\": \"2\", \"name\":\"Bbb\"}]";
NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
NSError *e = nil;
NSMutableArray *json = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&e];
NSLog(@"%@", json);
Run Code Online (Sandbox Code Playgroud)
(我不得不使用反斜杠转义JSON字符串中的引号.)
你的代码似乎很好,除了结果是一个NSArray,而不是一个NSDictionary,这是一个例子:
前两行只是使用JSON创建一个数据对象,就像从网上读取它一样.
NSString *jsonString = @"[{\"id\": \"1\", \"name\":\"Aaa\"}, {\"id\": \"2\", \"name\":\"Bbb\"}]";
NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
NSError *e;
NSMutableArray *jsonList = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&e];
NSLog(@"jsonList: %@", jsonList);
Run Code Online (Sandbox Code Playgroud)
NSLog内容(字典列表):
jsonList: (
{
id = 1;
name = Aaa;
},
{
id = 2;
name = Bbb;
}
)
Run Code Online (Sandbox Code Playgroud)
[{"id": "1", "name":"Aaa"}, {"id": "2", "name":"Bbb"}]
Run Code Online (Sandbox Code Playgroud)
在上面的JSON数据中,您显示我们有一个包含字典数量的数组.
您需要使用此代码进行解析:
NSError *e = nil;
NSArray *JSONarray = [NSJSONSerialization JSONObjectWithData: data options: NSJSONReadingMutableContainers error: &e];
for(int i=0;i<[JSONarray count];i++)
{
NSLog(@"%@",[[JSONarray objectAtIndex:i]objectForKey:@"id"]);
NSLog(@"%@",[[JSONarray objectAtIndex:i]objectForKey:@"name"]);
}
Run Code Online (Sandbox Code Playgroud)
快速3/3 +
//Pass The response data & get the Array
let jsonData = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as! [AnyObject]
print(jsonData)
// considering we are going to get array of dictionary from url
for item in jsonData {
let dictInfo = item as! [String:AnyObject]
print(dictInfo["id"])
print(dictInfo["name"])
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
214521 次 |
| 最近记录: |