JSON array and list conversion related issues

Source: Internet
Author: User

I've used all of the following two methods.

Method One:

String jstr= "{' json ': ' Jsonvalue ', ' bool ': true, ' int ': 1, ' Double ': ' 20.5 '}";
Jsonobject Json=jsonobject.fromobject (JSTR);
Boolean Bool=json.getboolean ("bool");
int i=json.getint ("int");
Double d=json.getdouble ("double");
String value=json.getstring ("json");
System.out.println ("bool=" +string.valueof (BOOL) + "\tjson=" +value+ "\tint=" +i+ "\tdouble=" +d);

If you have a Bean object
Class user{
private String name;
Private String PSW;
Encapsulation Getter/setter omitted
}
String u= "{' name ': ' sail331x ', ' PSW ': ' 123456789 '}";
User user= (user) Jsonobject.tobean (jsonobject.fromobject (U), user.class);
You can do it.
To turn a user into a JSON object:
Jsonobject juser=jsonobject.fromobject (user);
String jstr=juser.tostring ();//This becomes a JSON string.

Method Two:

Declare a person entity class:

Package HB;

Import Java.util.Date;

public class Person {

String ID;
int age;
String name;
Date birthday;
Public String getId () {
return ID;
}
public void SetId (String id) {
This.id = ID;
}
public int getage () {
return age;
}
public void Setage (int.) {
This.age = age;
}
Public String GetName () {
return name;
}
public void SetName (String name) {
THIS.name = name;
}
Public Date Getbirthday () {
return birthday;
}
public void Setbirthday (Date birthday) {
This.birthday = Birthday;
}

}

Testing JSON data transformations

Package HB;

Import Java.util.Iterator;
Import java.util.List;

Import Org.junit.Test;

Import Net.sf.json.JSONArray;
Import Net.sf.json.JsonConfig;

public class Jsontolist {

public static void Main (string[] args) {
String json= "[{' Name ': ' Huangbiao ', ' age ': 15},{' name ': ' Liumei ', ' Age ': 14}]";
Jsonarray Jsonarray = Jsonarray.fromobject (JSON);
System.out.println (Jsonarray);
List List = (list) jsonarray.tocollection (Jsonarray, Person.class);
Iterator it = List.iterator ();
while (It.hasnext ()) {
Person P = (person) it.next ();
System.out.println (P.getage ());
}
}

@Test
public void JsonToList1 () {
String json= "[{' Name ': ' Huangbiao ', ' age ': 15},{' name ': ' Liumei ', ' Age ': 14}]";
Jsonarray Jsonarray = Jsonarray.fromobject (JSON);
System.out.println (Jsonarray);
List List = (list) jsonarray.tolist (Jsonarray, Person.class);
Iterator it = List.iterator ();
while (It.hasnext ()) {
Person P = (person) it.next ();
System.out.println (P.getage ());
}

}

@Test
public void JsonToList2 () {
String json= "[{' Name ': ' Huangbiao ', ' age ': 15},{' name ': ' Liumei ', ' Age ': 14}]";
Jsonarray Jsonarray = Jsonarray.fromobject (JSON);
System.out.println (Jsonarray);
System.out.println ("------------");
List List = (list) jsonarray.tolist (Jsonarray, new Person (), new Jsonconfig ());
Iterator it = List.iterator ();
while (It.hasnext ()) {
Person P = (person) it.next ();
System.out.println (P.getage ());
}

}

}

To convert a list object to a JSON string array:

Package HB;

Import java.util.LinkedList;
Import java.util.List;

Import Net.sf.json.JSONArray;

public class Listtojson {

public static void Main (string[] args) {
List List = new LinkedList ();
for (int i=0;i<3;i++) {
Person p = new person ();
P.setage (i);
P.setname ("name" +i);
List.add (P);
}
Jsonarray Jsonarray = jsonarray.fromobject (list);
System.out.println (Jsonarray);
}

}

JSON array and list conversion related issues

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.