gpt4 book ai didi

iphone - 如何将 json 字符串解析为 nsdictionary?

转载 作者:太空狗 更新时间:2023-10-30 03:14:05 26 4
gpt4 key购买 nike

我正在为登录应用程序编写代码。谁能帮我解析 json 字符串?我的代码是

-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{

NSString *loginStatus = [[NSString alloc] initWithBytes: [webData mutableBytes] length:[webData length] encoding:NSUTF8StringEncoding];

SBJsonParser *parser = [[SBJsonParser alloc] init];

NSArray *loginDict = [parser objectWithString:loginDict error:nil];

[loginStatus release];

[connection release];

最佳答案

示例数据:

NSString *strData = @"{\"1\": {\"name\": \"Jerry\",\"age\": \"12\"}, \"2\": {\"name\": \"Bob\",\"age\": \"16\"}}";
NSData *webData = [strData dataUsingEncoding:NSUTF8StringEncoding];

NSError *error;
NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:webData options:0 error:&error];
NSLog(@"JSON DIct: %@", jsonDict);

NSLog 输出:

JSON DIct: {
1 = {
age = 12;
name = Jerry;
};
2 = {
age = 16;
name = Bob;
};
}

关于iphone - 如何将 json 字符串解析为 nsdictionary?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10041689/

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