#"SaveRegistrationIDResult" : #"{\"Table\":[{\"Return_Code\":1,\"Return_Message\":\"ALREADY REGISTERED\"}]}"
I'm finding it impossible to fetch data from this json string.
I'm getting the table Table Object as a NSDictionary using this code.
NSMutableDictionary *jsonData = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
NSData *processedJsondata = [jsonData objectForKey:#"SaveRegistrationIDResult"];
but I getting errors for everything i try ahead.
I'd like to fetch data from within the Table array ('Return_Code') and print it in a Log.
You are getting json string so you can parse it like,
id jsonString = #"{\"Table\":[{\"Return_Code\":1,\"Return_Message\":\"ALREADY REGISTERED\"}]}"; // or [yourDictionary objectForKey : #"SaveRegistrationIDResult"];
NSData *data = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
id jsonObject = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
Now you can get value of any key from above object like,
NSArray *arr = [jsonObject objectForKey : #"Table"];
NSLog(#"your result : %#", arr);
Lion's comment worked.
This is how I fetched the value. Any optimizations welcome.
NSMutableDictionary *jsonData = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
NSString *processedJsondata = [jsonData objectForKey:#"SaveRegistrationIDResult"];
NSData *tableData = [processedJsondata dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *tableJsonObject = [NSJSONSerialization JSONObjectWithData:tableData options:0 error:nil];
NSArray *tableArray = [tableJsonObject objectForKey:#"Table"];
NSDictionary *tableObject = [tableArray objectAtIndex:0];
NSString *reposnseCode = [tableObject objectForKey:#"Return_Message"];
NSLog(#"%#",reposnseCode);
How to convert string to dictionary.We are using NSJSON but getting nil value
NSString *decryptstr = [CTAES decrypt:text];
decryptstr contains below data
{
"status":"200","description":"OK","count":"4","reg_flag":1,"cars":
[],"appointments_count":"0","addbymecars":[{"gallery":
[],"imagescount":0,"id":"1924453","mypage_list_flag":0,"appointment_date":"",
"listing_status":"Active","sellername":"test ","sellernumber":"+918888888888","email":"ufhwifuw#gmail.com","make":"Hyundai","model":"i20","model_type":"Asta 1.4 CRDI","listing_type":"","listing_sorting_order":"","band_color":"","price":"65,59,595\/-","state":"Telangana","city":"Hyderabad","area":"","mfgyear":"2013","color":"Green","owner":"2","user_type":"person","dealer_id":"1503181","isfeatured":"0","fueltype":"Diesel","pincode":"0","mileage":"89,89,898","app_latitude":"","app_longitude":"","certification":"n","certification_text":"","images":{"ldpi":"http:\/\/imagecdn.cartrade.com\/notavailable80x60.jpg","mdpi":
"http:\/\/imagecdn.cartrade.com\/notavailable100x75.jpg","hdpi":"http:\/\/imagecdn.cartrade.com\/notavailable150x113.jpg","xhdpi":"http:\/\/imagecdn.cartrade.com\/notavailable200x150.jpg","xxhdpi":"http:\/\/imagecdn.cartrade.com\/notavailable300x225.jpg"},"usedcarshortlist":"0","postingdate":"04-Jan-2016","mobile_link":"http:\/\/testm.cartrade.com\/L1924453","transmission":
"Manual"},{"gallery":[],"imagescount":0,"id":"1924742","mypage_list_flag":0,"appointment_date":"","listing_status":"Active","sellername":"yuyu ","sellernumber":"+919898889989","email":"jhhgj#gmail.com","make":"Maruti Suzuki","model":"800","model_type":"AC","listing_type":"","listing_sorting_order":"","band_color":"","price":"15,64,654\/-","state":"Delhi","city":"New Delhi","area":"","mfgyear":"2014","color":"","owner":"1","user_type":"person","dealer_id":"1484907","isfeatured":"0","fueltype":"Petrol","pincode":"110001","mileage":"1,233","app_latitude":"28.6182","app_longitude":"77.2233",
"certification":"n","certification_text":"","images":{"ldpi":"http:\/\/imagecdn.cartrade.com\/notavailable80x60.jpg","mdpi":"http:\/\/imagecdn.cartrade.com\/notavailable100x75.jpg","hdpi":"http:\/\/imagecdn.cartrade.com\/notavailable150x113.jpg","xhdpi":"http:\/\/imagecdn.cartrade.com\/notavailable200x150.jpg","xxhdpi":"http:\/\/imagecdn.cartrade.com\/notavailable300x225.jpg"},"usedcarshortlist":"0","postingdate":"05-Feb-2016","mobile_link":"http:\/\/testm.cartrade.com\/L1924742","transmission":"Manual"},{"gallery":[],"imagescount":0,"id":"1924641","mypage_list_flag":0,"appointment_date":"","listing_status":"Active","sellername":"vcnvmcx ","sellernumber":"+919812345687","email":"chk#gmail.com","make":"Maruti Suzuki","model":"Alto 800","model_type":"LX","listing_type":"","listing_sorting_order":"",
"band_color":"","price":"2,50,000\/-","state":"Maharashtra","city":"Mumbai","area":"New Hyderabad","mfgyear":"2015","color":"","owner":"1","user_type":"person","dealer_id":"1503221","isfeatured":"0","fueltype":"Petrol","pincode":"500001","mileage":"50,000","app_latitude":"17.3897","app_longitude":"78.478","certification":"n","certification_text":"","images":{"ldpi":"http:\/\/imagecdn.cartrade.com\/notavailable80x60.jpg","mdpi":"http:\/\/imagecdn.cartrade.com\/notavailable100x75.jpg","hdpi":"http:\/\/imagecdn.cartrade.com\/notavailable150x113.jpg","xhdpi":"http:\/\/imagecdn.cartrade.com\/notavailable200x150.jpg","xxhdpi":"http:\/\/imagecdn.cartrade.com\/notavailable300x225.jpg"},
"usedcarshortlist":"1","postingdate":"03-Feb-2016","mobile_link":"http:\/\/testm.cartrade.com\/L1924641","transmission":"Manual"
}
]}
using NSJSON
NSError *error;
NSData *jsonData = [decryptstr dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *returndict = [NSJSONSerialization JSONObjectWithData:jsonData
options:NSJSONReadingMutableContainers
error:&error];
returndict getting nil value.
add this line before convert ur string
NSString *decryptstr = [CTAES decrypt:text];
decryptstr = [NSString stringWithFormat:#"%s",[decryptstr UTF8String]];
Your string doesn't contain any escape characters, which Objective-C cann't read directly. So add escape characters to your string using following snippet.
// *** Get your string data ***
NSString *decryptstr = [CTAES decrypt:text];
// *** Add Escape characters to your string ***
NSString *escaped = [decryptstr stringByReplacingOccurrencesOfString:#"\"" withString:#"\\\""];
NSError *error;
// *** Convert escaped string to Data ***
NSData *jsonData = [escaped dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *returndict = [NSJSONSerialization JSONObjectWithData:jsonData
options:NSJSONReadingMutableContainers
error:&error];
NSLog(#"%#",returndict);
I just created one JSON file and copied your JSON data.
NSString *filePath = [[NSBundle mainBundle]pathForResource:#"data" ofType:#"json"];
NSData *data = [NSData dataWithContentsOfFile:filePath];
NSError *err;
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&err];
How to convert string to JSON object or JSON array in iOS?
my JSON string Like That.
{"Data": [{"ID":"1","Name":"Raj"},{"ID":"2","Name":"Rajneesh"}]}
I want to get ID or Name from this string please help me if anyone now this.
Thank You.
I try below code but print null
NSString *JsonString=#"{"Data": [{"ID":"1","Name":"Raj"},{"ID":"2","Name":"Rajneesh"}]}";
NSData *objectData = [JsonString dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:objectData options:0 error:&error];
NSArray* array = [json objectForKey:#"Data"];
NSLog(#"Print Array %#",array);
Use this
NSString *str=#"{\"Data\": [{\"ID\":\"1\",\"Name\":\"Raj\"},{\"ID\":\"2\",\"Name\":\"Rajneesh\"}]}";
NSMutableDictionary *dict=[NSJSONSerialization JSONObjectWithData:[str dataUsingEncoding:NSUTF8StringEncoding] options:kNilOptions error:nil];
NSMutableArray *dataArr=[dict valueForKey:#"Data"];
for (NSDictionary *userData in dataArr) {
NSLog(#"Id:%# Name:%#",[userData valueForKey:#"ID"],[userData valueForKey:#"Name"]);
}
Always Remember that when there are { } curly brackets, it means it is Dictionary and when [ ] this, means Array
NSURL *url=[NSURL URLWithString:#"Your JSON URL"];
NSData *data = [[NSData alloc] initWithContentsOfURL:url];
NSMutableDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
NSArray *array = json[#"Data"];
for(NSMutableDictionary *dic in array)
{
NSLog(#"%#",dic[#"ID"]); // give 1 & 2
NSLog(#"%#",dic[#"Name"]); // Raj and Rajneesh
}
This is not the correct JSON string which can be parsed by Objective c, get string from encoder and you will get a valid string, other then that for JSON to Dictionary conversion is simple in iOS as its natively supported.
NSData *data = [strJSON dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *jsonResponse = [NSJSONSerialization JSONObjectWithData:data
options:kNilOptions
error:&error];
Problem: I can't parse data from a JSON file to a NSArray appropriately. UTF encoding is not working as expected.
My JSON looks something like:
[
{"Name":"Marcos","Address":"1234 Brasil Av. São Paulo - SP","Latitude":"-23.000","Longitude":"-46.70"},{"Name":"Mario","Address":"1000 Washignton Luiz Av. Itú SP","Latitude":"-20.0000","Longitude":"-46.000"}
]
My Objective-C code is:
NSError *error = nil;
NSURL *jsonUrl = [[NSURL alloc]initWithString:
#"http://marcosdegni.com.br/teste/webservice_teste.php"];
NSString *jsonString = [NSString stringWithContentsOfURL:jsonUrl
encoding:NSUTF8StringEncoding error:&error];
NSLog(#"jsonString: %# , Error:%#:" ,jsonString, error); //(1)
if (!error) {
NSError *error2 = nil;
NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
NSArray * jsonArray = [NSJSONSerialization JSONObjectWithData:jsonData options:kNilOptions error:&error2];
NSLog(#"\n\nArray: %#" \nError:$#, jsonArray, error2); //(2)
//(*1*) This log show the content's as they are expected: note the characters ã and ú on the address fields.
//(*2*) The logs from the array and the dictionary show this charters as it's UNIX codes:\U00e and \U00fa respectively.
You can give this a try. The id json you get will be a NSArray, you can use it from there.
NSData *data = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
id json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
NSArray * array = json;
for (NSDictionary *dict in array) {
NSString *string = [dict objectForKey:#"Address"];
NSLog(#"%#",string);
}
From here, and I get the right result if I obtain the value of the key and log it, instead of logging the NSArray directly.
#"{\"details\": \"A friend request is already pending.\"}".. This is a json string coming from the server. i want to retrieve the object in a one string i.e is A friend request is already pending string is to be in one string. please tell me how to retrieve this.
i tried like this
NSString *str = #"{\"details\": \"A friend request is already pending.\"}";
NSLog(#"%#",str);
NSString *resultStr1 = [str stringByReplacingOccurrencesOfString:#"{" withString:#""];
NSString *resultStr2 = [resultStr1 stringByReplacingOccurrencesOfString:#"}" withString:#""];
//NSLog(#"%#",resultStr2);
//NSString *encodestr = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
// NSLog(#"%#",encodestr);
NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];
data = [data subdataWithRange:NSMakeRange(0, [data length] - 1)];
NSLog(#"%#",data);
NSError *error =Nil;
NSArray *JSON = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
NSLog(#"%#",JSON);
NSLog(#"%#",[JSON valueForKey:#"details"]);
Try this. COnvert your string into a dictionary and access it by keys. This will be helpful when you come across large group of data.
NSString *str = #"{\"details\": \"A friend request is already pending.\"}";
NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];
NSError* error;
NSDictionary* dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
NSLog(#"%#", [dic objectForKey:#"details"]);
Try this may be it will help you
NSDictionary *JSON =
[NSJSONSerialization JSONObjectWithData: str dataUsingEncoding:NSUTF8StringEncoding]
options: NSJSONReadingMutableContainers
error: &e];
NSLog(#"%#",[JSON valueForKey:#"details"]);