Bancha Rojkittisakul
Bancha Rojkittisakul

Reputation: 166

iOS: How can I get JSON data with \" \ character?

I have JSON data in the following format:

{ "Data": "{ \"data\": { \"current_condition\": [ {\"cloudcover\": \"0\", \"humidity\": \"33\", \"observation_time\": \"07:40 AM\", \"precipMM\": \"0.0\", \"pressure\": \"1008\", \"temp_C\": \"29\", \"temp_F\": \"84\", \"visibility\": \"10\" } ] }}" }

I can get "Data" with

NSError *err=nil;
NSURL *url = [NSURL URLWithString:@"http://something.smthin.com"];
NSData *dataFromUrl = [NSData dataWithContentsOfURL:url];
NSDictionary *object = [NSJSONSerialization JSONObjectWithData:dataFromUrl options:kNilOptions error:&err];
NSDictionary* Data = [object objectForKey:@"Data"];

but I can't get to "temp_C" because I don't know how to get data like \"data\": or \"temp_C\":\"29\"

In this case,I want to get the value of \"temp_C"\ that is 29. How can I achieve this?

Upvotes: 0

Views: 104

Answers (3)

Jeffery Thomas
Jeffery Thomas

Reputation: 42588

Wow that is messed up. You have JSON with a single key and value.

The key is Data the value is a string: { "data": { "current_condition": [ {"cloudcover": "0", "humidity": "33", "observation_time": "07:40 AM", "precipMM": "0.0", "pressure": "1008", "temp_C": "29", "temp_F": "84", "visibility": "10" } ] }}.

So the value is a string that contains an inner JSON object.

You need to double deserialize this.

NSDictionary *JSON1 = [NSJSONSerialization JSONObjectWithData:dataFromUrl options: kNilOptions error:&err];
NSData *dataForData = [JSON1[@"Data"] dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *JSON2 = [NSJSONSerialization JSONObjectWithData:dataForData options: kNilOptions error:&err];

NSArray *celciusTemperatures = [JSON2 valueForKeyPath:@"data.current_condition.temp_C"];
NSString *firstCelciusTemperature = [celciusTemperatures firstObject];

Upvotes: 1

Martin R
Martin R

Reputation: 539725

For some reason, [object objectForKey:@"Data"] is not a dictionary but a string containing a JSON object for a dictionary. Therefore the following should work:

NSDictionary *object = [NSJSONSerialization JSONObjectWithData:dataFromUrl options:kNilOptions error:&err];
NSString* tmpString = [object objectForKey:@"Data"];
NSData *tmpData = [tmpString dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *data = [NSJSONSerialization JSONObjectWithData:tmpData options:kNilOptions error:&err];

Now data is a dictionary and you can access its contents, for example

NSDictionary *currentCondition = data[@"data"][@"current_condition"][0];
NSString *tempC = currentCondition[@"temp_C"];

Upvotes: 1

T.J. Crowder
T.J. Crowder

Reputation: 1074238

I can't imagine why, but what you have there is JSON defining a single object with a single property, Data, which has a string value. That string contains JSON. E.g., you have JSON wrapped in JSON.

The best answer is probably to fix the source so it doesn't return this odd nested structure.

However, to get at temp_C, first you decode the outer layer and get the string from Data. Then you decode that string to get the actual information, which looks like this:

{
    "data": {
        "current_condition": [
            {
                "cloudcover": "0",
                "humidity": "33",
                "observation_time": "07:40 AM",
                "precipMM": "0.0",
                "pressure": "1008",
                "temp_C": "29",
                "temp_F": "84",
                "visibility": "10"
            }
        ]
    }
}

From the resulting object, get the data property value (an object), then get that object's current_condition property (an array), get its first entry (an object), and get the temp_C value from that object.

Upvotes: 1

Related Questions