gpt4 book ai didi

ios - 在iOS中解析Json而没有常见的 key

转载 作者:行者123 更新时间:2023-12-01 19:01:29 26 4
gpt4 key购买 nike

我的JSON字符串是:

[
{'Local':'webaddress'},
{'QA':'webaddress1'}
]

我的代码是:
NSMutableDictionary *dataDictonary = [NSJSONSerialization
JSONObjectWithData:responseData
options:0
error:nil];


NSArray *keys = [dataDictonary allKeys];
NSArray *values = [dataDictonary allValues];
int i=0;
NSLog(@"",[keys count]);
NSLog(@"",[values count]);
int i=0;
for ( NSString *items in keys )
{
NSLog(@"----");
NSLog(@"Name: %@", items);
NSLog(@"Address: %@", values[i++]);
NSLog(@"----");
}

在这里,我得到的是大小,因为NSlog中没有空白,并且无法解析此值,请不要这样做。请帮忙..

最佳答案

您的JSON是一个内部包含对象的数组,因此您需要将其转换为NSArray:

NSString *json_string = @"[{\"Local\": \"webaddress\" }, {\"QA\": \"webaddress1\" }]";
NSError *error;
NSArray *JSON =
[NSJSONSerialization JSONObjectWithData: [json_string dataUsingEncoding:NSUTF8StringEncoding]
options: NSJSONReadingMutableContainers
error: &error];

NSLog(@"Local: %@", JSON[0][@"Local"]); // output is: Local: webaddress

更新
// itherate through array
for(NSDictionary *dictionary in JSON)
{
//now you can iterate throug each dicitonary
NSEnumerator *enumerator = [dictionary keyEnumerator];
id key;

while((key = [enumerator nextObject])){
NSLog(@"key=%@ value=%@", key, [dictionary objectForKey:key]);
}
}

日志如下所示:

键=本地值=网址

键=质量检查值=网址1

关于ios - 在iOS中解析Json而没有常见的 key ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22610075/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com