Android, PHP parses JSON array, jsonarray, jsonoject, garbled

Source: Internet
Author: User

Unicode-to-UTF-8, json_encode garbled Solution

Bytes --------------------------------------------------------------------------------------------------------------------------

I hope this will help passers-...

Some of the small projects need to communicate with the server, which is reflected in the android UI. Therefore, an efficient JSON

Thoughts:

The Code is as follows:

Public jsonarray getphonetopcategory () {string url = base_url + "? Option = gettopcate ";
/* Simulate a browser GET request */httpget request = new httpget (URL); jsonarray = NULL;/* stringbulder and jdk_1.5 are added. The usage is basically the same as that of stringbuffer, but the efficiency is much higher than stringbuffer */stringbuilder sbuilder = new stringbuilder (); try {/* simulate the browser client */httpclient client = gethttpclient ();
/* Obtain the client's response to the browser's request */httpresponse response=client.exe cute (request );
/* Test whether the request is successful */system. out. println (response. getstatusline (). getstatuscode (); If (response. getstatusline (). getstatuscode () == 200) {bufferedreader reader = new bufferedreader (New inputstreamreader (response. getentity (). getcontent (), "UTF-8"); For (string S = reader. readline (); s! = NULL; S = reader. readline () {// s = new string (S. getbytes ("Unicode"), "UTF-8"); sbuilder. append (s);} log. I ("json_str", sbuilder. tostring ());
// This part is worth mentioning. Because I return a two-dimensional array, we need to use the jsonarray string (the content is in the array format) constructor.
// It always appears that the printed Unicode code is a string because the decoding is incorrect. First, you must program the jsonarray object by using the constructor.
// Then, use the getjsonobject method of jsonarray to read the content one by one. In this way, JSON is correctly parsed without printing.
// Is a string of Unicode encoding problems, in addition, nonsense, the client, return JSON page must be UTF-8 encoding, only the UTF-8 supports JSON, if
// Non-UTF-8 encoding, must be converted to other encoding formats in advance to UTF-8 encoding, do not appear garbled or even JSON is empty phenomenon, PHP Other encoding to UTF-8
// You can search for more information on the Internet.
Jsonarray JSON= New jsonarray (sbuilder. tostring ());For (INT I = 0; I <JSON. length (); I ++) {system. out. println (JSON. getjsonobject (I ). getstring ("dname");} // jsonarray jssonarray = JSON. tojsonarray (jssonarray) ;}} catch (exception e) {e. printstacktrace ();} return jsonarray ;}

PHP terminal, code, PHP for UTF-8 encoding, can use Notepad ++ and other conversion encoding method

if($_POST['option']!=''){    switch($_POST['option'])    {        case 'GetTopCate':            $sql='select XX,XXX from XXX where Dpid=0';            $result=mysql_query($sql) or die(mysql_error());            $res=Array();            while($rows=mysql_fetch_assoc($result))            {                $res[]=$rows;            }            echo json_encode($res);            break;    }}

 

I feel that the foundation is not solid. First of all, I don't know the ability to read the JSON format is converted to unicode format. I think it is a encoding problem. I also don't know Java's jsonarray and jsonobject, too many accounts are overdue.

 

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.