ios - how to delete the Superfluous Escape-character in a NSString object -
i'm newbie development of ios. , when deal json nsjsonserialization , find problem me.
nslog(@"response: %@", responsestring); nsdata *jsondata = [responsestring datausingencoding:nsutf8stringencoding]; nsdictionary *dict = [nsjsonserialization jsonobjectwithdata:jsondata options:nsjsonreadingmutablecontainers error:nil]; nslog(@"dict: %@", dict);
and output is:
2013-03-18 20:13:56.228 xxxx[3550:5003] response: {"status":"success","data":"{\"title\":\"\",\"sessionname\":\"sid\",\"sessionid\":\"9217e5df3db6b4b4aa3eed800890069f\",\"rand\":5360}","md5":"292ee1e78628fc6360c647e938c4f1ea"} 2013-03-18 20:13:56.229 xxxx[3550:5003] dict: { data = "{\"title\":\"\",\"sessionname\":\"sid\",\"sessionid\":\"9217e5df3db6b4b4aa3eed800890069f\",\"rand\":5360}"; md5 = 292ee1e78628fc6360c647e938c4f1ea; status = success;
with "\" data section cannot nsdictionary object
so should make right?
sorry poor english.
for whatever reason, value of "data" not json dictionary, string containing json data. can fix applying jsonobjectwithdata
string again , replacing value in dictionary:
nsdata *jsondata = [responsestring datausingencoding:nsutf8stringencoding]; nsmutabledictionary *dict = [nsjsonserialization jsonobjectwithdata:jsondata options:nsjsonreadingmutablecontainers error:nil]; nsdata *nestedjsondata = [[dict objectforkey:@"data"] datausingencoding:nsutf8stringencoding]; nsdictionary *nesteddict = [nsjsonserialization jsonobjectwithdata:nestedjsondata options:nsjsonreadingmutablecontainers error:nil]; [dict setobject:nesteddict forkey:@"data"]; nslog(@"dict: %@", dict);
output:
dict: { data = { rand = 5360; sessionid = 9217e5df3db6b4b4aa3eed800890069f; sessionname = sid; title = ""; }; md5 = 292ee1e78628fc6360c647e938c4f1ea; status = success; }
Comments
Post a Comment