开发者

Issue with newline in text - UITextView to sqlite, sqlite to JSON (via NSString), JSON to PHP

开发者 https://www.devze.com 2023-03-05 12:23 出处:网络
I have a UITextView that ends up sending the text to a sqlite db on the iPhone.Later on I will be querying this data from sqlite, putting it into a JSON string and sending it to a server (PHP/MySQL) f

I have a UITextView that ends up sending the text to a sqlite db on the iPhone. Later on I will be querying this data from sqlite, putting it into a JSON string and sending it to a server (PHP/MySQL) from the iPhone using ASIHTTPRequest (using ASIFormDataRequest class).

There is a data condition where this send to PHP fails to decode properly (using json_decode). The data condition seems to be where there's a newline in the text that was entered to sqlite via the UITextView.

Things I've Tested:

  • Write out the same JSON string that I'm trying to send to the server to a text file on the iPhone. Take that text, copy it into a web form (that I put together for this troubleshooting) that POSTs the data to the same PHP script that accepts the JSON from the iPhone.开发者_Python百科 This processes successfully (e.g. decodes and writes to the MySQL db).
  • Write out the same JSON string that I'm trying to send to the server to the console (using NSLog). Copy the text from the console and copy it into a web form that POSTs the data to the same PHP script that accepts the JSON from the iPhone. This processes successfully (e.g. decodes and writes to the MySQL db).
  • Remove the newline in the sqlite db, run the app and send the JSON to the server per design, no problem...it decodes and writes to the MySQL db.

I want to keep the newline in the text as that is what the user typed and what they will expect when they see the information on the server. Not sure how to keep the newline and at the same time be able to send the data to the server without any issues.

Thanks,

Shawn


How are you creating the JSON string?

The newline should be encoded in the JSON as two characters: \n

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号