In iOS, how do I parse a JSON string into an object

前端 未结 7 835
日久生厌
日久生厌 2021-02-08 05:41

I come from Android dev, so sorry if I\'m missing obvious iOS concepts here.

I have a JSON feed that looks like:

{\"directory\":[{\"id\":0,\"fName\":\"...\

7条回答
  •  独厮守ぢ
    2021-02-08 06:32

    For this, you can SBJSON framework.

    You have to convert the response string into an NSDictionary like

     NSString *responseString = [[NSString alloc]initWithData:responseData encoding:NSUTF8StringEncoding];
        NSDictionary *dic = [responseString JSONValue];
    

    Now you can create an object for Staff class.

    Staff *staff = [[Staff alloc]init];
    

    Then you can store values in this object like

    staff.firstname = [[[dic objectForKey:@"directory"] objectAtIndex:0] objectForKey:@"fName"];
    

    Now you can pass this single object to other classes

提交回复
热议问题