JSON various conversions Summary

Source: Internet
Author: User

Convert between jsonobject,jsonarray,map,string
http://blog.csdn.net/superit401/article/details/51727739

1.String Turn Jsonobject

String jsonmessage = "{" Language ":" 88 "," Math ":" 78 "," Computer ":" 99 "}";

Jsonobject Myjson = Jsonobject.fromobject (jsonmessage);

2.String Turn Jsonarray

String jsonmessage = "[{' num ': ' Score ', ' foreign language ': 88, ' history ': 65, ' geography ': ' A ', ' object ': ' AAA ': ' 1111 ', ' BBB ': ' 2222 ', ' cccc ': ' 3333 '}}," +
"{' num ': ' Interest ', ' foreign language ': 28, ' history ': 45, ' geography ': +, ' object ': {' aaa ': ' 11a11 ', ' BBB ': ' 2222 ', ' cccc ': ' 3333 '}}," +
"{' num ': ' Hobby ', ' foreign language ': 48, ' History ': 62, ' geography ': ' The '": ' ' aaa ': ' 11c11 ', ' BBB ': ' 2222 ', ' cccc ': ' 3333 '}}] ";
Jsonarray Myjsonarray = Jsonarray.fromobject (jsonmessage);
System.out.println (Myjsonarray);

3.String Turn Map

(1)

String jsonmessage = "{" Language ":" 88 "," Math ":" 78 "," Computer ":" 99 "}";

Jsonobject Myjson = Jsonobject.fromobject (jsonmessage);

Map m = Myjson;

(2) (need to introduce Jackjson core, DataBind, annotations three jar packages)

String json = "{" "PayPal Key2": "PayPal value2", "PayPal Key1": "PayPal value1", "PayPal Key3": "PayPalvalue3"} ";

Objectmapper mapper = new Objectmapper ();
Map

(3) (string in special format)

String A = "{se=2016, Format=xml, at=en co=3}";

A = a.substring (1, A.length ()-1);
Map DocType = new HashMap ();
Java.util.StringTokenizer items;
for (StringTokenizer Entrys = new StringTokenizer (A, ","); Entrys.hasmoretokens ();
Doctype.put (Items.nexttoken (), Items.hasmoretokens ()? ((Object) (Items.nexttoken ())): null) {
Items = new StringTokenizer (Entrys.nexttoken (), "=");
}

4.JSONObject, Jsonarray,map turn string

Jsonobject--string:

System.out.println (myjsonobject);//can directly output the contents of Jsonobject

Myjsonobject.tostring ();

Jsonarray--string:

System.out.println (Myjsonarray);//can directly output the contents of Myjsonarray

Myjsonarray.tostring ();

Map--string:

SYSTEM.OUT.PRINTLN (map);//can directly output the contents of the map

Map.tostring ();

5.JSONObject Turn Jsonarray

6.JSONObject Turn Map

Jsonobject Myjson = Jsonobject.fromobject (jsonstring);

Map m = Myjson;

7.JSONArray Turn Jsonobject

for (int i=0; i < myjsonarray.length (); i++)
{
Get each of the Jsonobject objects
Jsonobject Myjobject = Myjsonarray.getjsonobject (i);

}

8.JSONArray Turn Map

9.Map Turn Jsonobject

Jsonobject JSON = jsonobject.fromobject (map);

10.Map Turn Jsonarray

Jsonarray.fromobject (map);

11.List Turn Jsonarray
Jsonarray jsonArray2 = jsonarray.fromobject (list);

12.JSONArray Goto List

List

13.String Turn Array

String string = "A,b,c";
String [] stringarr= string.split (","); Note that separators are needed to translate the drops ...
If it is a string like "abc", it is directly
String string = "abc";
char [] Stringarr = String.tochararray (); Note The return value is a char array
If you want to return a byte array, use the GetBytes method directly OK ~ ~
String string = "abc";
byte [] Stringarr = String.getbytes ();

Array to String

Char[] Data={a,b,c};
String S=new string (data);

JSON various conversions Summary

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.