0

My APP gets a JSON string from a api call JSON string has objects and a array in it. 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 :

{
   "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];
2
  • 2
    I think you got dictionary not array...so just simply try [json objectForKey:@"Id"]; Commented Nov 3, 2015 at 4:32
  • 1
    A side note, you should not use [NSData dataWithContentsOfURL:url] to get the data from a network resource. It will block the GUI. Commented Nov 3, 2015 at 4:46

3 Answers 3

4

your json is not array so parse like following

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"];
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot . Works Perfect !
0

Your Json Object is NSDictionary. so you can directly get data using 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.

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. it will help you in future. Hope this will help you.

1 Comment

Thanks a lot . Works Perfect !
0

you can do it by following way.

Your Json is in NSDictionary format.

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

    }

1 Comment

Thanks much better :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.