一,JSON 格式有几种
1,对象形式
{
"test":{
},
"test1":[
{
}
]
}
即:对象包对象,以及数组。对象是不能直接包对象的,比如{{}}。
2,数组形式
[
{
"test":"",
"test1":""
}
]
即:数组包对象,其中数组中直接是对象{},不能是key value形式(["key":{}] 错误的结构)
二,普通字符产和json串
1,普通字符,在Java中就是被""包裹的字符串,比如 String str = "This is e string!";
2,json串,必须有key value的形式在里面,比如 String str = "{id : '23dswer',name:'obhee'}"
三, Json和Java对象的转换用法
Json文件
{
"Persons": [
{
"Id": "1",
"name": "This is name1",
"type": "2",
"address": "深圳市1"
},
{
"Id": "2",
"name": "This is name2",
"type": "2",
"address": "深圳市2"
},
{
"Id": "3",
"name": "This is name3",
"type": "2",
"address": "深圳市3"
}
]
}
Java 转化
public static void main(String[] args) {
String s = readJsonFile("F:\\E\\test.json");
JSONObject jobj = JSON.parseObject(s);
List<Person> Persons = JSON.parseArray(jobj.getString("Persons"), Person.class);
for (int i = 0; i < Persons.size(); i++) {
Person key = Persons.get(i);
System.out.println(key.toString());
}
}
结果:


博客介绍了JSON格式,包括对象形式(对象包对象及数组)和数组形式(数组包对象),区分了普通字符串和JSON串,普通字符串是Java中被引号包裹的,JSON串需有键值对。还提及了JSON和Java对象的转换用法。
392

被折叠的 条评论
为什么被折叠?



