gpt4 book ai didi

ios - 带数组 iOS 的 JSON 字符串

转载 作者:行者123 更新时间:2023-11-28 18:55:44 25 4
gpt4 key购买 nike

我的应用程序从 api 调用中获取一个 JSON 字符串 JSON 字符串中包含对象 和一个数组。这是我到目前为止所做的,但我无法从中获得值(value)。请给我建议,我是 iOS 新手。这是我的 JSON 字符串:

{
"Id":"0d95a9f6-c763-4a31-ac6c-e22be9832c83",
"Name":"john",
"ProjectName":"project1",
"StartDate":"\/Date(1447200000000)\/",
"Documents":
[{
"Id":"2222a","Name":"book1","ContentType":"application/pdf"
},
{
"Id":"3718e","Name":"Toolbox","ContentType":"application/fillform"
}]
}

代码

NSString *URLString = [NSString stringWithFormat:@"http://mysite/API/Assignments?"];
NSURL *url = [NSURL URLWithString:URLString];
NSData *data=[NSData dataWithContentsOfURL:url];
json=[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];

assignArray=[[NSMutableArray alloc]init];

for (int i=0; i<json.count; i++) {
NSString *aID=[[json objectAtIndex:i]objectForKey:@"Id"];
NSString *uName=[[json objectAtIndex:i]objectForKey:@"Name"];
NSString *pName=[[json objectAtIndex:i]objectForKey:@"ProjectName"];

//[self initwithUserID:uID userName:uName proName:pName];
// [self retrieveAssignmentDetails:aID];
AssignmentsJson *assignment=[[AssignmentsJson alloc]initwithassignID:aID userName:uName proName:pName];
[assignArray addObject:assignment];

最佳答案

你的 json 不是数组,所以解析如下

NSString *aID = json[@"Id"];
NSString *uName = json[@"Name"];
NSString *pName = json[@"ProjectName"];
NSString *startDate = json[@"StartDate"];

NSArray *documents = json[@"Documents"];
for (NSDictionary *item in documents) {
NSString *itemID = item[@"Id"];
NSString *itemName = item[@"Name"];
NSString *itemContentType = item[@"ContentType"];
}

关于ios - 带数组 iOS 的 JSON 字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33491413/

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