Java parsing PHP function json_encode Unicode encoding problem _java

Source: Internet
Author: User

In Android development, there is a coding problem when docking with the server-side interface, and the data obtained from the server is "\u8bbe\u59071id-\u8bbe\u59071\u540d\u79f0;\u8bbe\u59073id-\u8bbe\ The u59073\u540d\u79f0;\u8bbe\u59077id-\u8bbe\u59077\u540d\u79f0 "interface is returned by encoding the Json_encode in the PHP function. In the client through the Java.net.URLdecoder.decode () decoding is not used, but directly copy the above string to the decode () method can be normal decoding, the received string after Utf-8 code after the use, and finally search the relevant information on the Internet to find a solution.

One, Json_encode function:

json_encode-the variable with JSON encoding.

Description: String json_encode ($value) that returns the JSON form of value values.

Parameter: The value to be encoded, except for the resource type, can be any data type

The function can only accept UTF-8 encoded data (= character/string type of data)

Return value: The successful encoding returns a string in the form of JSON.

Second, the client decodes in the Java language:

The first of these methods

public string Unescapeunicode (String str) {
  stringbuffer b=new stringbuffer ();
  Matcher m = Pattern.compile ("\\\\u ([0-9a-fa-f]{4})"). Matcher (str);
  while (M.find ())
   B.append ((char) Integer.parseint (M.group (1));
  return b.tostring ();
 }

Just use the Unescapeunicode () method to decode it.

2. Using Json_simple.jar packet parsing

Download Address: http://www.jb51.net/softs/455885.html

Json.simple is a simple Java class library for parsing and generating JSON text. Does not depend on other class libraries, high performance.

Object Obj=jsonvalue.parse (JSONSTR); return obj.tostring ();


PHP Server-side workaround:

 

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.