- JSON字符串数组转化为JSONArray
/*** 解析JSON字符串数组,中括号表示的是数组,大括号表示的是对象* @param args*/
public static void main(String[] args) { //JSON.parseArray的作用等同于JSONArray.parseArray String jsonString = "[{\"type\":\"Boolean\",\"value1\":\"true\",\"value2\":\"false\"}]"; JSONArray jsonArray = JSON.parseArray(jsonString); System.out.println("jsonArray:" + jsonArray);
JSONArray jsonArray1 = JSONArray.parseArray(jsonString); System.out.println("jsonArray1:" + jsonArray1);
for (int i = 0; i < jsonArray.size(); i++) { System.out.println("1" + jsonArray.get(i)); JSONObject jsonObject = (JSONObject) jsonArray.get(i); System.out.println("2" + jsonObject);
String type = jsonObject.getString("type"); System.out.println("type:" + type);
String value = jsonObject.getString("value1"); System.out.println("value1:" + value);
String mayvalue = jsonObject.getString("value2"); System.out.println("value2:" + mayvalue); }}
- JSON字符串对象解析为JSONArray抛出异常
/*** 这里会抛出异常,因为parseArray接收的不是一个数组* @param args*/
public static void main(String[] args) { String jsonString = "{\"type\":\"Boolean\",\"value1\":\"true\",\"value2\":\"false\"}"; JSONArray jsonArray = JSON.parseArray(jsonString); System.out.println("jsonArray:" + jsonArray);
for (int i = 0; i < jsonArray.size(); i++) { System.out.println("1" + jsonArray.get(i)); JSONObject jsonObject = (JSONObject) jsonArray.get(i); System.out.println("2" + jsonObject);
String type = jsonObject.getString("type"); System.out.println("type:" + type);
String value = jsonObject.getString("value1"); System.out.println("value1:" + value);
String mayvalue = jsonObject.getString("value2"); System.out.println("value2:" + mayvalue); }}
抛出异常如下:
- JSON字符串数组解析成JSONObject抛出异常
/*** 这里也会抛出异常,jsonArray不能强转成jsonObject* @param args*/
public static void main(String[] args) { String jsonString = "[{\"type\":\"Boolean\",\"value1\":\"true\",\"value2\":\"false\"}]";
JSONObject jsonObject = JSON.parseObject(jsonString); System.out.println("jsonObject:" + jsonObject);}
抛出异常如下:
- JSON字符串对象解析成JSONObject
JSON.parseObject和JSONObject.parseObject的使用也尽量在项目中统一,基本功能一样。 /*** 解析JSON对象,JSON.parseObject等同于JSONObject.parseObject* @param args*/
public static void main(String[] args) {
String jsonString = "{\"type\":\"Boolean\",\"value1\":\"true\",\"value2\":\"false\"}";
JSONObject jsonObject = JSON.parseObject(jsonString); System.out.println("jsonObject:" + jsonObject);
JSONObject jsonObject1 = JSONObject.parseObject(jsonString); System.out.println("jsonObject1:" + jsonObject1);}
- POJO对象,List集合,Map集合转化为JSONString
public class Person {
private String name; private String age;
public Person() { }
public Person(String name, String age) { this.name = name; this.age = age; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public String getAge() { return age; }
public void setAge(String age) { this.age = age; }
@Override public String toString() { return "Person{" + "name='" + name + '\'' + ", age='" + age + '\'' + '}'; }}
下面将会分别实现Person对象、List<String>、List<Person>、Map<String, String>、Map<String, Person>转化为JSONString(其实就是String),使用的方法是JSON.toJSONString.
/*** 这里的案例是把对象转化成JSON字符串;* @param args*/
public static void main(String[] args) {
//单个对象转化为JSONString Person person01 = new Person("chenyufeng","14");
String s = JSON.toJSONString(person01); System.out.println("s:" + s);
//List对象转化为JSONString List<String> list1 = new ArrayList<String>(); list1.add("1111"); list1.add("2222"); list1.add("3333");
String s3 = JSON.toJSONString(list1); System.out.println("s3:" + s3);
//List对象转化为JSONString Person person02 = new Person("chenyufeng2","142"); Person person03 = new Person("chenyufeng3","143"); List<Person> list = new ArrayList<Person>(); list.add(person01); list.add(person02); list.add(person03);
String s1 = JSON.toJSONString(list); System.out.println("s1:" + s1);
//Map对象转化为JSONString Map<String, String> map = new HashMap<String, String>(); map.put("1", "chenyufeng"); map.put("2", "gaowenjing"); String s2 = JSON.toJSONString(map); System.out.println("s2:" + s2);
//Map对象转化为JSONString Map<String, Person> map1 = new HashMap<String, Person>(); map1.put("1", person01); map1.put("2", person02); map1.put("3", person03); String s4 = JSON.toJSONString(map1); System.out.println("s4:" + s4);}
- JSON字符串对象转化为POJO类对象
/*** JSON字符串可以转化为POJO类对象;* @param args*/
public static void main(String[] args) {
Person person = new Person("chenyufeng", "12"); String s = JSON.toJSONString(person); System.out.println("s:" + s);
Person person1 = JSON.parseObject(s, Person.class); System.out.println("person:" + person1.toString());}
- JSON字符串数组对象转化为List<T>对象
/*** JSON数组字符串转化为List<T>对象;* @param args*/
public static void main(String[] args) { Person person = new Person("chen", "12"); Person person1 = new Person("yufeng","24");
List<Person> list = new ArrayList<Person>(); list.add(person); list.add(person1);
String s = JSON.toJSONString(list); System.out.println("s:" + s);
List<Person> list1 = JSON.parseArray(s, Person.class); System.out.println("list1:" + list1.toString());}