Android parsing Unicode encoding

Source: Internet
Author: User

Objective

When we get JSON information from the network, sometimes we get something that is not well known, such as the following is the JSON information I get:

Can see artist_name I want to be the singer's name, the result is a bunch of things do not know what the code, I guess, it may be Unicode encoding, because it is similar to Unicode encoding, for two reasons:

    • The basic format of Unicode is the form of->\u2345, always 4 numbers and letters, and the length of a given string is a multiple of 4, which makes it very possible that it is Unicode encoding.
    • UTF-8 encoding is a bunch of unknown things that give a string length that does not conform (exclusion method)

Now that I'm guessing it's Unicode, I'm going to verify that using a Unicode-encoded conversion tool, I get the following result:

It seems that my guess is correct, then how to use code to parse it, can not hand use tools to parse it, this is certainly not, so I still Baidu a bit (I do not understand ~ ~), and finally got some answers.

Realize

The idea of implementation is still relatively clear, the code is not very difficult, the amount of ~ ~ should be said to be very simple bar, see the know.

    1. The resulting string is split every four of a set
    2. Use the integer class method to get the four set of strings to the corresponding decimal size int value (PS: The original four is a group of 16 binary number Ah, looked very similar)
    3. Converts an int value to a character of type Char
    4. Convert char type to string
    5. Connecting each string you get is the parsed content.

Obviously we need to write a function, here is my code, I do not do too much explanation, is very simple content.

 string  Getstrfromunicode (string  Unicode) {string         str = ; for  (int  i=0 ; I<unicode.length (); I+=4 ) {string  s =  "; for  (int  j=i;j<i+4 ; j + +)            {S+=string . ValueOf (Unicode.charat (j)); } str+=string . ValueOf ((char) integer.valueof (S, 16<        /span>). Intvalue ());    } return str; }

After I parse it, the results of the program run are as follows:

Can see, the program is running correctly, the visible method is still right, although I do not know whether there is a relevant already encapsulated method can be used directly, but now the first to do so.

Summary

After this experience, I found that encountered problems or can not be too anxious, according to their own study of the content of bold speculation, sometimes may be the content is not at all, but perhaps is right, ^_^!

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Android parsing Unicode encoding

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.