简体   繁体   English

带有iOS数组的JSON字符串

[英]JSON String with Arrays iOS

My APP gets a JSON string from a api call JSON string has objects and a array in it. 我的APP从api调用获取JSON字符串JSON字符串中包含对象数组 This is what i have done so far but i couldn't get the values from it . 到目前为止,这是我所做的,但是我无法从中获得价值。 advice me please and I'm new to iOS .This is my JSON String : 请给我建议,我是iOS的新手。这是我的JSON String:

{
   "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"
   }]
}

Code

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];

your json is not array so parse like following 您的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"];
}

Your Json Object is NSDictionary . 您的Json对象是NSDictionary so you can directly get data using valueForKey 这样您就可以使用valueForKey直接获取数据

NSDictionary *json=[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
NSString *aID = json[@"Id"];
NSString *uName = json[@"Name"];
NSString *pName = json[@"ProjectName"];

for Id , Name and ContentType is inside your array object within your NSDictionary object. 对于IdNameContentType在NSDictionary对象内的数组对象内。

so you can get those values accessing array index. 因此您可以获得访问数组索引的那些值。

NSArray *arr = json[@"Documents"];
for (int i=0; i<arr.count; i++) {
    NSString *aID=[[arr objectAtIndex:i]objectForKey:@"Id"];
    NSString *uName=[[arr objectAtIndex:i]objectForKey:@"Name"];
    NSString *cType=[[arr objectAtIndex:i]objectForKey:@"ContentType"];
}

You should learn NSArray and NSDictionary Structure first. 您应该首先学习NSArrayNSDictionary Structure。 it will help you in future. 它将对您有所帮助。 Hope this will help you. 希望这会帮助你。

you can do it by following way. 您可以按照以下方式进行操作。

Your Json is in NSDictionary format. 您的JsonNSDictionary格式。

 NSData * data = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://mysite/API/Assignments?"]];
    NSDictionary * dicResponse = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];


    if(dicResponse){
        NSString * strID = [dicResponse valueForKey:@"Id"];
        NSString * strName = [dicResponse valueForKey:@"Name"];
        NSString * strProjectName = [dicResponse valueForKey:@"ProjectName"];
        NSString * strDate = [dicResponse valueForKey:@"StartDate"];
        NSArray * arrDocuments = [NSArray arrayWithArray:[dicResponse valueForKey:@"Documents"]];

        for (int i=0; i< arrDocuments.count; i++) {

            NSString * ID=[[arrDocuments objectAtIndex:i]valueForKey:@"Id"];
            NSString * Name=[[arrDocuments objectAtIndex:i]valueForKey:@"Name"];
            NSString * Type=[[arrDocuments objectAtIndex:i]valueForKey:@"ContentType"];
        }

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM