JSON Parsing in iOS 7

2019-01-13 00:16发布

I am creating an app for as existing website. They currently has the JSON in the following format :

[

   {
       "id": "value",
       "array": "[{\"id\" : \"value\"} , {\"id\" : \"value\"}]"
   },
   {
       "id": "value",
       "array": "[{\"id\" : \"value\"},{\"id\" : \"value\"}]"
   } 
]

which they parse after escaping the \ character using Javascript.

My problem is when i parse it in iOS using the following command :

NSArray *result = [NSJSONSerialization JSONObjectWithData:jsonData options:kNilOptions error:&localError];

and do this :

NSArray *Array = [result valueForKey:@"array"];

Instead of an Array I got NSMutableString object.

  • The website is already in production so I just cant ask them to change their existing structure to return a proper JSON object. It would be a lot of work for them.

  • So, until they change the underlying stucture, is there any way i can make it work in iOS like they do with javascript on their website?

Any help/suggestion would be very helpful to me.

14条回答
爷、活的狠高调
2楼-- · 2019-01-13 01:01

May be this will help you.

- (void)jsonMethod
{
    NSMutableArray *idArray = [[NSMutableArray alloc]init];
    NSMutableArray *nameArray = [[NSMutableArray alloc]init];
    NSMutableArray* descriptionArray = [[NSMutableArray alloc]init];

    NSHTTPURLResponse *response = nil;
    NSString *jsonUrlString = [NSString stringWithFormat:@"Enter your URL"];
    NSURL *url = [NSURL URLWithString:[jsonUrlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];


    NSURLRequest *request = [[NSURLRequest alloc]initWithURL:url];
    NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];

    NSDictionary *result = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:nil];
    NSLog(@"Result = %@",result);


    for (NSDictionary *dic in [result valueForKey:@"date"])
    {
        [idArray addObject:[dic valueForKey:@"key"]];
        [nameArray addObject:[dic valueForKey:@"key"]];
        [descriptionArray addObject:[dic valueForKey:@"key"]];

    }

}
查看更多
看我几分像从前
3楼-- · 2019-01-13 01:02
-(void)responsedata
{

    NSMutableURLRequest *request=[[NSMutableURLRequest alloc]initWithURL:[NSURL URLWithString:replacedstring]];

    [request setHTTPMethod:@"GET"];
    NSURLSessionConfiguration *config=[NSURLSessionConfiguration defaultSessionConfiguration];
    NSURLSession *session=[NSURLSession sessionWithConfiguration:config];
    NSURLSessionDataTask *datatask=[session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        if (error) {
            NSLog(@"ERROR OCCURE:%@",error.description);
        }
        else
        {
            NSError *error;
            NSMutableDictionary *responseDict=[[NSMutableDictionary alloc]init];
            responseDict=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error];


            if (error==nil)
            {


             // use your own array or dict for fetching as per your key..  


                _responseArray =[[NSMutableArray alloc]init];

                _geometryArray=[[NSMutableArray alloc]init];



                _responseArray=[responseDict valueForKeyPath:@"result"];

                referncestring =[[_photosArray objectAtIndex:0]valueForKey:@"photo_reference"];

                _geometryArray=[_responseArray valueForKey:@"geometry"];
               // _locationArray=[[_geometryArray objectAtIndex:0]valueForKey:@"location"];
                _locationArray=[_geometryArray valueForKey:@"location"];
                latstring=[_locationArray valueForKey:@"lat"];
                lngstring=[_locationArray valueForKey:@"lng"];


        coordinates = [NSMutableString stringWithFormat:@"%@,%@",latstring,lngstring];




            }

        }

        dispatch_sync(dispatch_get_main_queue(), ^
                      {



                         // call the required method here..

                      });




    }];
    [datatask resume];   //dont forget it

    }
查看更多
登录 后发表回答