这个适合初学者:刚刚我学习的
1.基础的“JSON”
对象是用一对大括号;
例如:
{
“name”:"xiaohua",
"age" : 23 ,
"susses": true
}
2.数组或者LIST
用一对中括号:
["a","b","c"]
3.对象数组
[{"name": "userName","age": 23}]
在使用的过程重,可以使用网站进行验证 http://www.bejson.com/
例如:有一个人的类,年龄,名字,老婆,老婆也有一个名字,有2个孩子,第一个孩子name, 第二个孩子name,第二个孩子有一个女儿,女儿也有一个名字属性。
{ "name": "perName", "age": 23, "wife": { "wifeName": "wifeName" }, "son": [ { "son1": { "name": "son1" }, "son2": { "name": "son2" }, "dau": { "dauName": "dauName" } } ] }
3. 使用IDEA工具进行序列化和反序列化。
具体详细的写法大家可以进行百度
public class JsonTest { @Test public void TestJson() { Person person = new Person(); person.setAge(18); person.setName("老王"); Wife wife = new Wife(); wife.setName("小王"); List<Son> list = new ArrayList(); List list1 =new LinkedList(); Son son = new Son(); son.setName("大明"); Son son1 = new Son(); son1.setName("小明"); NvEr nvEr = new NvEr(); nvEr.setName("女儿"); son1.setNvEr(nvEr); list.add(son); list.add(son1); person.setWife(wife); person.setSons(list); String json = JSON.toJSONString(person, SerializerFeature.WriteClassName); System.out.println(json); Person person1 = JSON.parseObject(json, Person.class); List<Son> sonList = person1.getSons(); for (Son son2 : sonList) { System.out.println(son2.getName()); } System.out.println(); } } 结果: { "@type": "com.longteng.json.Person", "age": 18, "name": "老王", "sons": [ { "@type": "com.longteng.json.Son", "name": "大明" }, { "@type": "com.longteng.json.Son", "name": "小明", "nvEr": { "name": "女儿" } } ], "wife": { "name": "小王" } }