您的位置:首页 > Web前端 > JavaScript

JSON构造和解析json-lib(一)

2015-07-03 02:19 597 查看
要介绍用json-lib构造和解析Json数据,主要分为两部分介绍:1、介绍将list map bean string array转换为JSON数据格;2、将JSON转化为list map bean形式。

下面链接包含了使用json-lib构造和解析Json数据的所有相关jar包和该文介绍使用的所有代码

所有jar包:



一、将list map bean string array转换为JSON数据格

1、将list转化为json数据格式使用方法看下面代码

package com.sd.test.tojson;

import java.util.ArrayList;
import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class ListToJson {
public static void main(String args[]){
ListToJson l2j=new ListToJson();
l2j.list2Json();
}
public void list2Json(){
List<String> list=new ArrayList<String>();
list.add("one");
list.add("two");
JSONArray list2json=JSONArray.fromObject(list);
//不能用下面的转换
//JSONObject list2json1=JSONObject.fromObject(list);

//System.out.println(list2json);
System.out.println(list2json.toString());
//System.out.println(list2json1);
//System.out.println(list2json1.toString());
}
}
输出结果:["one","two"]


2、将map转化为json数据格式使用方法看下面代码

package com.sd.test.tojson;

import java.util.HashMap;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class MapToJson {

public static void main(String[] args) {
// TODO Auto-generated method stub
MapToJson m2j=new MapToJson();
m2j.map2json();

}
public void map2json(){
Map<String,String> map=new HashMap<String,String>();
map.put("one","dog");
map.put("two","cat");
map.put("three","pig");
map.put("four","chicken");
map.put("five","cattle");

JSONObject map2json=JSONObject.fromObject(map);
JSONArray map2json1=JSONArray.fromObject(map);

System.out.println(map2json);
System.err.println(map2json1);
}

}


输出结果:

{“four”:”chicken”,”one”:”dog”,”two”:”cat”,”three”:”pig”,”five”:”cattle”}

[{“four”:”chicken”,”one”:”dog”,”two”:”cat”,”three”:”pig”,”five”:”cattle”}]

3、将bean转化为json数据格式使用方法看下面代码其中用到Student类的代码在下面有

package com.sd.test.tojson;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.sd.test.modle.Student;

public class BeanToJson {

public static void main(String[] args){
BeanToJson b2j=new BeanToJson();
b2j.bean2json();

}
public void bean2json(){
Student student1=new Student();

student1.setName("xiao ming");
student1.setAge(20);
student1.setStrudent(true);
JSONObject bean2json=JSONObject.fromObject(student1);
JSONArray bean2json1=JSONArray.fromObject(student1);

System.out.println(bean2json);
//System.out.println(bean2json.toString());
System.err.println(bean2json1);
//System.err.println(bean2json1.toString());
}

}


输出结果:

{“age”:20,”name”:”xiao ming”,”sex”:”“,”strudent”:true}

[{“age”:20,”name”:”xiao ming”,”sex”:”“,”strudent”:true}]

4、将String转化为json数据格式使用方法看下面代码

package com.sd.test.tojson;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class StringToJson {

public static void main(String[] args) {
// TODO Auto-generated method stub
StringToJson s2j=new StringToJson();
s2j.string2json();
}
public void string2json(){
String s="{\"four\":\"chicken\",\"one\":\"dog\",\"two\":\"cat\",\"three\":\"pig\",\"five\":\"cattle\"}";
String s1="[{\"four\":\"chicken\",\"one\":\"dog\",\"two\":\"cat\",\"three\":\"pig\",\"five\":\"cattle\"}]";
String s2="{\"age\":21,\"list\":[\"one\",\"two\"],\"list1\":[{\"age\":20,\"name\":\"xiao ming\",\"sex\":\"boy\",\"strudent\":true},{\"age\":23,\"name\":\"xiao hong\",\"sex\":\"girl\",\"strudent\":true}],\"map\":{\"one\":{\"age\":20,\"name\":\"xiao ming\",\"sex\":\"boy\",\"strudent\":true},\"two\":{\"age\":23,\"name\":\"xiao hong\",\"sex\":\"girl\",\"strudent\":true}},\"name\":\"da jun\",\"sex\":\"boy\",\"strudent\":false,\"student\":null}";
JSONObject string2json=JSONObject.fromObject(s);
//不能用JSONObject去解以【中括号】开始的字符串
//JSONObject string2json1=JSONObject.fromObject(s1);
JSONObject string2json12=JSONObject.fromObject(s2);

//不能用JSONArray解以{大括号}开始的字符串
//JSONArray string2json2=JSONArray.fromObject(s);
JSONArray string2json3=JSONArray.fromObject(s1);

System.out.println(string2json);
//System.out.println(string2json1);
System.out.println(string2json12);

//System.err.println(string2json2);
System.err.println(string2json3);

}

}


输出结果:

{“four”:”chicken”,”one”:”dog”,”two”:”cat”,”three”:”pig”,”five”:”cattle”}

{“age”:21,”list”:[“one”,”two”],”list1”:[{“age”:20,”name”:”xiao ming”,”sex”:”boy”,”strudent”:true},{“age”:23,”name”:”xiao hong”,”sex”:”girl”,”strudent”:true}],”map”:{“one”:{“age”:20,”name”:”xiao ming”,”sex”:”boy”,”strudent”:true},”two”:{“age”:23,”name”:”xiao hong”,”sex”:”girl”,”strudent”:true}},”name”:”da jun”,”sex”:”boy”,”strudent”:false,”student”:null}

[{“four”:”chicken”,”one”:”dog”,”two”:”cat”,”three”:”pig”,”five”:”cattle”}]

5、将Array转化为json数据格式使用方法看下面代码

package com.sd.test.tojson;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class ArrayToJson {
public static void main(String[] args){
ArrayToJson a2j=new ArrayToJson();
a2j.array2json();

}
public void array2json(){
//静态初始化数组:方法一
/* String name[] = new String[] {
"Tom","Sam","Mimi"
};*/

//静态初始化数组:方法二
//String name[] = {"Jimmy","Gougou","Doggy"};

//动态初始化数据
String name[] = new String[2];
name[0] = "Thinking in Java";
name[1] = "Effective Java";
//下面这种方法不行
// JSONObject array2json=JSONObject.fromObject(name);
JSONArray array2json1=JSONArray.fromObject(name);

//System.out.print(array2json.toString());
System.err.print(array2json1.toString());
}
}


输出结果:

[“Thinking in Java”,”Effective Java”]

6、将beans转化为json数据格式使用方法看下面代码其中用到Student和StudentWithLMBS类的代码在下面

package com.sd.test.tojson;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.sd.test.modle.Student;
import com.sd.test.modle.StudentWithLMBS;

public class ComplexToJson {

public static void main(String[] args) {
// TODO Auto-generated method stub
ComplexToJson c2j=new ComplexToJson();
c2j.complexTest();

}
public void complexTest(){
Student student=new Student();
Student student1=new Student();
StudentWithLMBS lmbs=new StudentWithLMBS();
List<String> list=new ArrayList<String>();
List<Object> list2=new ArrayList<Object>();
Map<String,Object> map=new HashMap<String,Object>();

student.setName("xiao ming");
student.setAge(20);
student.setStrudent(true);
student.setSex("boy");

student1.setName("xiao hong");
student1.setAge(23);
student1.setStrudent(true);
student1.setSex("girl");

map.put("one", student);
map.put("two", student1);

list.add("one");
list.add("two");

list2.add(student);
list2.add(student1);

lmbs.setAge(21);
lmbs.setName("da jun");
lmbs.setSex("boy");
lmbs.setStrudent(false);
lmbs.setMap(map);
lmbs.setList(list);
lmbs.setList1(list2);

JSONObject complextest=JSONObject.fromObject(lmbs);

JSONArray complextest1=JSONArray.fromObject(lmbs);

System.out.println(complextest);
System.out.println(complextest1);

}
}


输出结果:

{“age”:21,”list”:[“one”,”two”],”list1”:[{“age”:20,”name”:”xiao ming”,”sex”:”boy”,”strudent”:true},{“age”:23,”name”:”xiao hong”,”sex”:”girl”,”strudent”:true}],”map”:{“one”:{“age”:20,”name”:”xiao ming”,”sex”:”boy”,”strudent”:true},”two”:{“age”:23,”name”:”xiao hong”,”sex”:”girl”,”strudent”:true}},”name”:”da jun”,”sex”:”boy”,”strudent”:false,”student”:null}

[{“age”:21,”list”:[“one”,”two”],”list1”:[{“age”:20,”name”:”xiao ming”,”sex”:”boy”,”strudent”:true},{“age”:23,”name”:”xiao hong”,”sex”:”girl”,”strudent”:true}],”map”:{“one”:{“age”:20,”name”:”xiao ming”,”sex”:”boy”,”strudent”:true},”two”:{“age”:23,”name”:”xiao hong”,”sex”:”girl”,”strudent”:true}},”name”:”da jun”,”sex”:”boy”,”strudent”:false,”student”:null}]

上述用到的两个bean对象

student类:

package com.sd.test.modle;

public class Student {
private String name;
private String sex;
private int age;
private boolean isStrudent;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public boolean isStrudent() {
return isStrudent;
}
public void setStrudent(boolean isStrudent) {
this.isStrudent = isStrudent;
}
}


StudentWithLMBS类

package com.sd.test.modle;

import java.util.List;
import java.util.Map;

public class StudentWithLMBS {
private String name;
private String sex;
private int age;
private boolean isStrudent;
private List<String> list;
private List<Object> list1;
private Map<String,Object> map;
private Student student;

public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public boolean isStrudent() {
return isStrudent;
}
public void setStrudent(boolean isStrudent) {
this.isStrudent = isStrudent;
}
public Student getStudent() {
return student;
}
public void setStudent(Student student) {
this.student = student;
}
public List<String> getList() {
return list;
}
public void setList(List<String> list) {
this.list = list;
}
public Map<String, Object> getMap() {
return map;
}
public void setMap(Map<String, Object> map) {
this.map = map;
}
public List<Object> getList1() {
return list1;
}
public void setList1(List<Object> list1) {
this.list1 = list1;
}

}


二、将JSON转化为list map bean形式

1、将json转化为list代码如下:

package com.sd.test.parsejson;

import java.util.ArrayList;
import java.util.List;

import net.sf.json.JSONArray;

public class JsonToList {

public static void main(String[] args) {
// TODO Auto-generated method stub
JsonToList j2l=new JsonToList();
j2l.jsontolist();
}

//@SuppressWarnings("deprecation")
public void jsontolist(){
String jlist="[\"one\",\"two\"]";
List<String> list=new ArrayList<String>();
List<String> list2=new ArrayList<String>();
JSONArray js=JSONArray.fromObject(jlist);
//方法一该方法不推荐将来可能被废弃
list=js.toList(js);
System.out.println(list);
//方法二
for(int i=0;i<js.size();i++){
list2.add(js.getString(i));
}
System.err.println(list);

}
}


输出结果:

[one, two]

[one, two]

2、将JSON转化为map代码如下

package com.sd.test.parsejson;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import net.sf.json.JSONObject;

public class JsonToMap {

public static void main(String[] args) {
// TODO Auto-generated method stub
String mj="{\"four\":\"chicken\",\"one\":\"dog\",\"two\":\"cat\",\"three\":\"pig\",\"five\":\"cattle\"}";
JSONObject stojson=JSONObject.fromObject(mj);
Map<String,String> map=new HashMap<String,String>();
Iterator<String> it=stojson.keys();
//System.out.println(stojson.keys());
while(it.hasNext()){
String key=(String)it.next();
String value=(String)stojson.get(key);
map.put(key, value);
}
System.out.println(map);

}

}


输出结果:

{four=chicken, one=dog, two=cat, three=pig, five=cattle}

3、将JSON转化为bean代码如下

package com.sd.test.parsejson;

import net.sf.json.JSONObject;

import com.sd.test.modle.Student;

public class JsonToBean {

public static void main(String[] args) {
// TODO Auto-generated method stub
JsonToBean j2b=new JsonToBean();
j2b.jsontobean();
}
public void jsontobean(){
String beanjson="{\"age\":20,\"name\":\"xiao ming\",\"sex\":\"\",\"strudent\":true}";
Student student=new Student();

JSONObject beanj=JSONObject.fromObject(beanjson);
student =(Student)beanj.toBean(beanj,Student.class);

System.err.println(student.getName());
}

}


输出结果:

xiao ming

4、将复杂的SON数据解析仅供参考:

package com.sd.test.parsejson;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.sd.test.modle.Student;
import com.sd.test.modle.StudentWithLMBS;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class ParseJson {
public static void main(String[] args){
ParseJson pj=new ParseJson();
pj.parseJson();
}
public void parseJson(){
String objjson="{\"age\":21,\"list\":[\"one\",\"two\"],\"list1\":[{\"age\":20,\"name\":\"xiao ming\",\"sex\":\"boy\",\"strudent\":true},{\"age\":23,\"name\":\"xiao hong\",\"sex\":\"girl\",\"strudent\":true}],\"map\":{\"one\":{\"age\":20,\"name\":\"xiao ming\",\"sex\":\"boy\",\"strudent\":true},\"two\":{\"age\":23,\"name\":\"xiao hong\",\"sex\":\"girl\",\"strudent\":true}},\"name\":\"da jun\",\"sex\":\"boy\",\"strudent\":false,\"student\":null}";
String arrayjson="[{\"age\":21,\"list\":[\"one\",\"two\"],\"list1\":[{\"age\":20,\"name\":\"xiao ming\",\"sex\":\"boy\",\"strudent\":true},{\"age\":23,\"name\":\"xiao hong\",\"sex\":\"girl\",\"strudent\":true}],\"map\":{\"one\":{\"age\":20,\"name\":\"xiao ming\",\"sex\":\"boy\",\"strudent\":true},\"two\":{\"age\":23,\"name\":\"xiao hong\",\"sex\":\"girl\",\"strudent\":true}},\"name\":\"da jun\",\"sex\":\"boy\",\"strudent\":false,\"student\":null}]";
Student student=new Student();
StudentWithLMBS swlmbs=new StudentWithLMBS();

JSONObject objj=JSONObject.fromObject(objjson);

JSONArray arrayj=JSONArray.fromObject(arrayjson);

//student =(Student)objj.toBean(objj.getJSONArray("list1").getJSONObject(1),Student.class);
student =(Student)JSONObject.toBean(objj.getJSONArray("list1").getJSONObject(1),Student.class);
System.err.println(student.getName());
swlmbs=(StudentWithLMBS)objj.toBean(objj,StudentWithLMBS.class);
//swlmbs=(StudentWithLMBS)JSONObject.toBean(objj,StudentWithLMBS.class);
System.out.println(swlmbs.getList().toString());

if(objj.has("list")){

JSONArray jarray=objj.getJSONArray("list");
//System.out.println(jarray.getString(1));
List<String> list=new ArrayList<String>();
for(int i=0;i<jarray.size();i++){
list.add(jarray.getString(i));
//System.out.println(jarray.getString(i));
}
System.err.println(list.toString());
}

}
}


输出结果:

xiao hong

[one, two]

[one, two]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java json