Tuesday, 17 September 2013

Error in converting NSArray to JSON

Error in converting NSArray to JSON

I have NSArray with NSDictionaries inside. I need to post JSON. So I tried
to make post request with NSURLConnection, but I'm given an error (bad
url). I convert NSArray such direction:
NSData *dishesData = [NSJSONSerialization dataWithJSONObject:dishes
options:NSJSONWritingPrettyPrinted error:&error];
NSString *dishesString = [[NSString alloc] initWithData:dishesData
encoding:NSUTF8StringEncoding];
Then paste it to NSString with request.
NSString *requestString = [NSString
stringWithFormat:@"%@%@=%@&%@=%@&%@=%@&%@=%@&%@=%@&%@=%@&%@=%@",
SERVER_ADDRESS, ACTION, ORDER_ACTION, ORDER_NAME, name, ORDER_EMAIL,
email, ORDER_PHONE, phone, ORDER_DELIVERY, delivery, ORDER_ADDRESS,
address, ORDER_DISHES, dishesString];
Then I log 1st time:
2013-09-17 20:01:13.926 Eda.by[3088:c07]
http://eda.by/api.php?act=order&name=Òèìóð&email=bernikowich@testemail2.com&phone=+375296802009&delivery=1&address=Òåñòîâûé
àäðåñ!&dish=[
{
"count" : 2,
"id" : 86
}
]
If I'll try to post this I'll get error "bad url". Thats why I try to
encode this by:
requestString = [requestString
stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
After that I get such logs:
http://eda.by/api.php?act=order&name=%D0%A2%D0%B8%D0%BC%D1%83%D1%80&email=bernikowich@testemail2.com&phone=+375296802009&delivery=1&address=%D0%A2%D0%B5%D1%81%D1%82%D0%BE%D0%B2%D1%8B%D0%B9%20%D0%B0%D0%B4%D1%80%D0%B5%D1%81!&dish=%5B%0A%20%20%7B%0A%20%20%20%20%22count%22%20:%202,%0A%20%20%20%20%22id%22%20:%2086%0A%20%20%7D%0A%5D
It sends well, but data are wrong and won't work.

No comments:

Post a Comment