Example of converting list array to json Array in java

Source: Internet
Author: User
Tags json

Example 1

The code is as follows: Copy code

Package com. yq1012.fastjson;
Import java. util. ArrayList;
Import com. alibaba. fastjson. JSON;
Import com. alibaba. fastjson. JSONArray;
Import com. alibaba. fastjson. JSONObject;

Public class TestListTojson {
Public static void main (String [] args ){
  
// [{"Title": "Good Voice of China" },{ title: "China Mobile online business hall" },{ title: "Bank of China" },{ title: "China Mobile" },{ title: "China haopin Phase III" },{ title: "China haopin Phase I" },{ title: "China Telecom online business office "}, {title: "Industrial and Commercial Bank of China" },{ title: "China haopin Phase II" },{ title: "China map"}]
  
JSONArray array = new JSONArray ();
ArrayList <String> list = new ArrayList <String> ();
List. add ("good voice in China ");
List. add ("Voice of China 1 ");
List. add ("Voice of China 2 ");
List. add ("Voice of China 3 ");
For (String string: list ){
JSONObject json = new JSONObject ();
Json. put ("title", string );
Array. add (json );
  }
System. out. println (array. toString ());
  
 }

}

Example 2

Convert a string array in json format to a List object

The code is as follows: Copy code

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 age ){
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;
 }
 
}
 


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': 'hangzhou', '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': 'hangzhou', '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': 'hangzhou', '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 ());
  }
  
 }

}
 

Convert the list object into a JSON string array

 

The code is as follows: Copy code


Package hb;

Import java. util. Collections list;
Import java. util. List;

Import net. sf. json. JSONArray;

Public class ListToJson {

Public static void main (String [] args ){
List list = new vertex List ();
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 );
 }

}
Print results

Java code
1. [{"age": 0, "birthday": null, "id": "", "name": "name0" },{ "age": 1, "birthday": null, "id": "", "name": "name1" },{ "age": 2, "birthday": null, "id ": "", "name": "name2"}]

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.