getting media url from json string
How would I get the image path+name out of this json?
/*
{
distance = 0;
"effective_from" = "2011-02-19 12:27:20";
"effective_thru" = "2011-02-19 13:27:20";
fname = eric;
latitude = "11.92179";
"listing_id" = 60;
lname = cartman;
longitude = "-74.70189";
lstatus = active;
media = "{\"media\":
[
{\"id\":149,\"media_url\":\"mediauploads/149.jpg\"},
{\"id\":150,\"media_url\":\"mediauploads/150.jpg\"},
{\"id\":151,\"media_url\":\"mediauploads/151.jpg\"},
{\"id\":152,\"media_url\":\"mediauploads/152.jpg\"}
]}";
tags = "Separate multiple tags with commas";
text = "Listing text";
title = "Please provide a descriptive title";
username = eric;
"users_id" = 13;
},
*/
My problem is that NSDictionary *media = [userContent objectForKey:@"media"]; returns a -[NSCFString objectForKey:]: unrecognized selector sent to instance 0x66662a0 so I开发者_Python百科 cannot get the Array out of it.
Update: so it turns out that I get as a string from the server. How can I parse it to a dictionary?
Any help is appreciated.
Thanks!
TouchJSON provides a JSON Deserializer class that contains a method to convert JSON to a dictionary.
If you get the JSON back as a string, firstly convert it to NSData. For example:
NSData *jsonData = [jsonString dataUsingEncoding:NSUTF32BigEndianStringEncoding];
Then create a CJSONDeserializer and pass the data to return a dictionary:
CJSONDeserializer *deserializer = [CJSONDeserializer deserializer];
error = nil;
NSDictionary *dict = [deserializer deserializeAsDictionary:jsonData error:&error];
You should then be able to extract your required data from the dictionary.
精彩评论