iOS returned is not in JSON format, it is NSString

Source: Internet
Author: User

Nsdictionary *result = value[@ "Data"];

if (![ Result Iskindofclass:[nsdictionary class]]) {

NSLog (@ "Data is not a dictionary");

if ([Result Iskindofclass:[nsstring class]]) {

NSData *data = [(NSString *) result datausingencoding: nsutf8stringencoding]; //Convert data format to UTF8

if (data) {

result = [Nsjsonserialization jsonobjectwithdata:data options:nsjsonreadingallowfragments Error: NULL];

} else {

result = @{};

}

NSLog (@ "Parse data:%@", result);

} else {

result = @{};

}

}

Each dictionary is placed inside the array

Commentlist = "[{\" user_id\ ": \" 1\ ",

\ "content\": \ "[\\u998b\\u5634][\\u6293\\u72c2]\",

\ "createtime\": \ "1970-01-01 08:00:00\",

\ "realname\": \ "\\u90d1\\u98de\"},

{\ "user_id\": \ "1\",

\ "content\": \ "[\\u563b\\u563b][\\u563b\\u563b][\\u563b\\u563b]\\u641e\\u5b9a\", \ "createtime\": \ "1970-01-01 08:00:00\ ", \" realname\ ": \" \\u90d1\\u98de\ "}, {\" user_id\ ": \" 1\ ", \" content\ ": \" \\u641e\\u 5b9a\ ", \" createtime\ ": \" 1970-01-01 08:00:00\ ", \" realname\ ": \" \\u90d1\\u98de\ "}]";

Nsdictionary *result = value[@ "Data"];

if (![ Result Iskindofclass:[nsdictionary class]]) {

NSLog (@ "Data is not a dictionary");

if ([Result Iskindofclass:[nsstring class]]) {

NSData *data = [(NSString *) result datausingencoding:nsutf8stringencoding];//convert data format to UTF8

}}

NSData *resdata = [[NSData alloc] initwithdata:[@ "[{\" user_id\ ": \" 1\ ", \" content\ ": \" [\\U998B\\U5634][\\U6293\\U72C2 ]\ ", \" createtime\ ": \" 1970-01-01 08:00:00\ ", \" realname\ ": \" \\u90d1\\u98de\ "},{\" user_id\ ": \" 1\ ", \" content\ ": \" [\ \u563b\\u563b][\\u563b\\u563b][\\u563b\\u563b]\\u641e\\u5b9a\ ", \" createtime\ ": \" 1970-01-01 08:00:00\ ", \" Realname\ ": \" \\u90d1\\u98de\ "},{\" user_id\ ": \" 1\ ", \" content\ ": \" \\u641e\\u5b9a\ ", \" createtime\ ": \" 1970-01-01 08:00:00\ ", \" realname\ ": \" \\u90d1\\u98de\ "}]" datausingencoding:nsutf8stringencoding] ";

Nsmutabledictionary *detaildic;

Nsarray *arr = [nsjsonserialization jsonobjectwithdata:resdata options:nsjsonreadingmutableleaves Error:nil];

int count = Arr.count;

Nsmutablearray *temparray = [Nsmutablearray arraywithcapacity:count];

for (int i = 0; i < count; i + +) {

Detaildic = [arr objectatindex:i];

Testobject *testobject = [Testobject new];

testobject.user_id = [Detaildic objectforkey:@ "user_id"];

testobject.user_id = [Detaildic objectforkey:@ "content"];

testobject.user_id = [Detaildic objectforkey:@ "Createtime"];

testobject.user_id = [Detaildic objectforkey:@ "Realname"];

[Temparray Addobject:testobject];

}

iOS returned is not in JSON format, it is NSString

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.