On the method of converting string into JSON array in thinkphp

Source: Internet
Author: User
Tags php compare

This is part of the code:
$client = M ("client");
$data = $client->where (' user_id = '. $user _id)->select ();
if ($data = = False | | $data = = NULL)
{
/* Query Error */
$str = Array (
' Status ' = ' ERROR ',
' msg ' = ' query error ',
' content ' = ' null '
);
$this->ajaxreturn ($STR);

}
Else
{
/* Query Successful */
$str = Array (
' Status ' = ' success ',
' msg ' = ' query succeeded ',
' Content ' = $data
);
$this->ajaxreturn ($STR);
}
Note: PHP compare two strings try not to use = = to compare, there will be unexpected consequences, try to use strcmp to compare, when the result is 0 when the expression wants to wait.
Here are two kinds of results:
1, the existence of users:
{"Status": "Success", "MSG": "\u67e5\u8be2\u6210\u529f", "content": [{"client_id": "000003", "Client_name": "\u5f90\ u5229\u5175 "," Client_phone ":" 18813752547 "," client_address ":" \u5e7f\u4e1c\u97f6\u5173\u7fc1\u6e90 "," Client_ Picture ":" Http:\/\/www.baidu.com "," user_id ":" 18813752547 "},{" client_id ":" 000002 "," Client_name ":" \u5f90\u5229\ u5175 "," Client_phone ":" 18813752547 "," client_address ":" \u5e7f\u4e1c\u97f6\u5173\u7fc1\u6e90 "," Client_picture ":" Http:\/\/www.baidu.com "," user_id ":" 18813752547 "},{" client_id ":" 000001 "," Client_name ":" \u5f90\u5229\u5175 "," Client_phone ":" 18813752547 "," client_address ":" \u5e7f\u4e1c\u97f6\u5173\u7fc1\u6e90 "," client_picture ":" Http:\/\ /www.baidu.com "," user_id ":" 18813752547 "},{" client_id ":" 000000 "," Client_name ":" \u5f90\u5229\u5175 "," Client_ Phone ":" 18813752547 "," client_address ":" \u5e7f\u4e1c\u97f6\u5173\u7fc1\u6e90 "," client_picture ":" http:\/\/ Www.baidu.com "," user_id ":" 18813752547 "},{" client_id ":" 000004 "," Client_name ":" \u5f90\u5229\u5175 "," Client_phone ":" 18813752547 "," client_addresS ":" \u5e7f\u4e1c\u97f6\u5173\u7fc1\u6e90 "," client_picture ":" Http:\/\/www.baidu.com "," user_id ":" 18813752547 "},{ "client_id": "000005", "Client_name": "\u5f90\u5229\u5175", "Client_phone": "18813752547", "client_address": "\u5e7f\ U4e1c\u97f6\u5173\u7fc1\u6e90 "," client_picture ":" Http:\/\/www.baidu.com "," user_id ":" 18813752547 "}]}
2, does not exist the user's:
{"Status": "Error", "MSG": "\u67e5\u8be2\u9519\u8bef", "content": "Null"}
I believe we all care about a problem: How does the Android client parse this JSON format? Let me answer for you:
Assuming that the Android client will save the content read from the server in Buider, then:
Jsonobject Jsonobject1 = new Jsonobject (builder.tostring ()). Getjsonobject ("status");
Jsonobject jsonobject2 = new Jsonobject (builder.tostring ()). Getjsonobject ("msg");
Jsonobject jsonobject3 = new Jsonobject (builder.tostring ()). Getjsonobject ("content");
String client_id = jsonobject3.getstring ("client_id");
String client_name = jsonobject3.getstring ("Client_name");
............
One analogy.
For other formats JSON parsing, self-search online method.
Please refer to this: http://www.cnblogs.com/cpcpc/archive/2011/07/22/2122989.html

On the method of converting string into JSON array in thinkphp

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.