ios - How to convert UTF8String to JSON based NSDictionary? -
i getting 1 of web-service response in json. here utf8string base responsestring:
"{\"applicationid\":1,\"currentposition\":0,\"endoffile\":false,\"media\":{\"active\":true,\"description\":\"this video demonstrates quick overview of bentley learn server.\",\"locationid\":1,\"mp4url\":\"http:\\/\\/cdnllnw.bentley.com\\/s\\/learnvideo\\/welcome.mp4?s=1380034262&e=1380077462&h=c2641922fd862ffe5c5a05e94786359a\",\"mediaid\":5003235,\"mediaurl\":\"http:\\/\\/cdnllnw.bentley.com\\/s\\/learnvideo\\/welcome.mp4?s=1380034262&e=1380077462&h=c2641922fd862ffe5c5a05e94786359a\",\"streamserver\":null,\"title\":\"welcome\",\"transferstatus\":0,\"filesize\":9094429},\"mediaduration\":0,\"skippedcurrentposition\":0,\"userbp\":8011512,\"viewedtime\":0}"
after formatting looks like:
{ "applicationid":1, "currentposition":0, "endoffile":false, "media": { "active":true, "description":"this video demonstrates quick overview of bentley learn server.", "locationid":1, "mp4url":"http:\/\/cdnllnw.bentley.com\/s\/learnvideo\/welcome.mp4?s=1380035311&e=1380078511&h=d8de8ade046266fb7324be90a575f978", "mediaid":5003235, "mediaurl":"http:\/\/cdnllnw.bentley.com\/s\/learnvideo\/welcome.mp4?s=1380035311&e=1380078511&h=d8de8ade046266fb7324be90a575f978", "streamserver":null, "title":"welcome", "transferstatus":0, "filesize":9094429 }, "mediaduration":0, "skippedcurrentposition":0, "userbp":8011512, "viewedtime":0 }
how can convert string json object or nsdictionary object can access key-values easily.
i have tried this:
nsstring *responsestring = [nsstring stringwithutf8string:response.c_str()]; nsdictionary *jsondict = [nsjsonserialization jsonobjectwithdata: [responsestring datausingencoding:nsutf8stringencoding] options: nsjsonreadingmutablecontainers error: &error];
but says jsondict nil , error description is:
error domain=nscocoaerrordomain code=3840 "the operation couldn’t completed. (cocoa error 3840.)" (json text did not start array or object , option allow fragments not set.) userinfo=0xd8bbd30 {nsdebugdescription=json text did not start array or object , option allow fragments not set.}
please help
adding nsjsonreadingallowfragments option should according error:
nsstring* jsonstring = @"{\"applicationid\":1,\"currentposition\":0,\"endoffile\":false,\"media\":{\"active\":true,\"description\":\"this video demonstrates quick overview of bentley learn server.\",\"locationid\":1,\"mp4url\":\"http:\\/\\/cdnllnw.bentley.com\\/s\\/learnvideo\\/welcome.mp4?s=1380034262&e=1380077462&h=c2641922fd862ffe5c5a05e94786359a\",\"mediaid\":5003235,\"mediaurl\":\"http:\\/\\/cdnllnw.bentley.com\\/s\\/learnvideo\\/welcome.mp4?s=1380034262&e=1380077462&h=c2641922fd862ffe5c5a05e94786359a\",\"streamserver\":null,\"title\":\"welcome\",\"transferstatus\":0,\"filesize\":9094429},\"mediaduration\":0,\"skippedcurrentposition\":0,\"userbp\":8011512,\"viewedtime\":0}"; nserror* error = nil; nsdictionary *jsondict = [nsjsonserialization jsonobjectwithdata: [responsestring datausingencoding:nsutf8stringencoding] options: nsjsonreadingmutablecontainers | nsjsonreadingallowfragments error: &error];
but means nsstring pasted in question contains characters not following json format. if add more code take value (file, web) - can more make valid. until treat answer workaround :)
the formatted string should be:
{\"applicationid\":1,\"currentposition\":0,\"endoffile\":false,\"media\":{\"active\":true,\"description\":\"this video demonstrates quick overview of bentley learn server.\",\"locationid\":1,\"mp4url\":\"http:\\/\\/cdnllnw.bentley.com\\/s\\/learnvideo\\/welcome.mp4?s=1380034262&e=1380077462&h=c2641922fd862ffe5c5a05e94786359a\",\"mediaid\":5003235,\"mediaurl\":\"http:\\/\\/cdnllnw.bentley.com\\/s\\/learnvideo\\/welcome.mp4?s=1380034262&e=1380077462&h=c2641922fd862ffe5c5a05e94786359a\",\"streamserver\":null,\"title\":\"welcome\",\"transferstatus\":0,\"filesize\":9094429},\"mediaduration\":0,\"skippedcurrentposition\":0,\"userbp\":8011512,\"viewedtime\":0}
without " @ beggining , @ end. maybe that's reason. if loading web - make sure don't have js included or headers , confirm web service responds json format in headers.
Comments
Post a Comment