JSON与字符串、javaBean、map集合的转化及json文件读取

目录

1.下载json库
2.引入json
1.Map-->JSON对象
2.JavaBean-->JSON
3.String-->JSON
4.文件--->JSON
5.生成json文件
JSON数组
6.String类型的JSON转换为JSON数组
7.map-->JSONArray
8.JSONArray->map

1.下载json库
http://www.json.org/ -- 下载JSON-java
2.引入json

a.先将源码打成jar包,然后导进去
b.直接使用java文件

1.Map-->JSON对象

Map map = new HashMap();
map.put("s01", "zs");
map.put("s02", "ls");
map.put("s03", "wmz");

JSONObject json = new JSONObject(map);
System.out.println(json);
  1. JavaBean-->JSON

     Person per = new Person();
     per.setName("zs");
     per.setAge(22);
     Address address = new Address("xian", "beiji");
     per.setAddress(address);
     
     JSONObject json = new JSONObject(per);
     System.out.println(json);
    

3.String-->JSON

String jsonStr = "{\"张三\":23,\"李四\":25,\"王麻子\":65,\"王五\":65}";
JSONObject json = new JSONObject(jsonStr);
System.out.println(json);

4.文件--->JSON

//1.文件-->字符串StringBuffer

File file = new File("src\\per.json");
FileInputStream fileInput = new FileInputStream(file);
byte[] bs = new byte[10];
int len = -1;
StringBuffer sb = new StringBuffer();
while ((len = fileInput.read(bs)) != -1) {
    String str = new String(bs, 0, len);
    sb.append(str);
}
fileInput.close();

//2.字符串StringBuffer-->String
String s = sb.toString();

//3.String-->JSON
JSONObject json = new JSONObject(s);
System.out.println(json);

5.生成json文件

Map map = new HashMap();
Person p1 = new Person("zs", 23, new Address("xa", "xj"));
Person p2 = new Person("ls", 64, new Address("bj", "hc"));
Person p3 = new Person("ww", 94, new Address("bj", "hc"));
map.put("zs", p1);
map.put("ls", p2);
map.put("ww", p3);

// map--->JSON
JSONObject json = new JSONObject(map);
Writer writer = new FileWriter("src\\jsonper.json");
json.write(writer);
writer.close();

JSON数组

[
    {"name":"zs","age":23},
    {"classname":"lq","classno":1},
    {"schoolname":"xj","zone":"xj"}
]

一些操作需要使用第三方夹包, jsonutil夹包在java8中被废弃,所以使用json-lib夹包

6.String类型的JSON转换为JSON数组

String jsonStr ="[{\"name\":\"zs\",\"age\":23},{\"classname\":\"lq\",\"classno\":1},{\"schoolname\":\"xj\",\"zone\":\"xj\"}]";
JSONArray jArray = new JSONArray(jsonStr);
System.out.println(jArray);

7.map-->JSONArray

Map map = new HashMap();
map.put("key1", "value1");
map.put("key2", "value2");
map.put("key3", "value3");

// JSONArray jsonArray = new JSONArray(map);会报 org.json.JSONException
// 需要使用第三方夹包json-lib
// jsonArray.append("key4", "value4");
// System.out.println(jsonArray);

// 使用json-lib库的JSONArray
net.sf.json.JSONArray jArray = new net.sf.json.JSONArray();
jArray = jArray.fromObject(map);
System.out.println(jArray);

8.JSONArray->map

JSONArray--->获取每一个json-->key:value-->map
//准备JSON数组数据
String jArrayStr = "["
          + "{\"name\":\"zs\",\"age\":23},"
          + "{\"classname\":\"lq\",\"classno\":1},"
          + "{\"schoolname\":\"xa\",\"zone\":\"xj\"}"
       + "] " ;
net.sf.json.JSONArray jArray = new net.sf.json.JSONArray();
jArray = jArray.fromObject(jArrayStr);

Map  map = new HashMap();

for(int i=0;imap
    Set keys = json.keySet();//每个json的所有Key
    for(String key:keys) {//遍历每个json的所有key
        Object value = json.get(key);
        map.put(key, value) ;
    }
}

System.out.println(map);

你可能感兴趣的:(JSON与字符串、javaBean、map集合的转化及json文件读取)