{ "firstName": "John", "lastName": "Smith", "sex": "male", "age": 25, "address": { "streetAddress": "21 2nd Street", "city": "New York", "state": "NY", "postalCode": "10021" }, "phoneNumber": [ { "type": "home", "number": "212 555-1234" }, { "type": "fax", "number": "646 555-4567" } ] }

JSON接口以 k-v 形式组成

数据结构

Object 结构体 {} : key必须是string类型(以双引号包装),value可以为任何基本类型或数据结构。

Array 数组 [], : 中括号 [] 起始,并用逗号 , 分割元素。

基本类型

string

number

true

false

null

 

JSON的数据演示

{
//json并不能使用//注释
//{}开始编辑 //key: name, value: Que。使用,进行分割 "name" : "Que", "age" : "12", //json中没有时间/日期等数据格式,可以用数字时间戳表示日期 "birthday":"1970-01-01", “school”: "kk college", "major": ["computer science", "infomation science"], "has_a_job": true, "real_estate": null }

JSON的使用

improt org.json

JSON数据的生成-使用put生成

private static void JSONObject(){

  JSONObject jsonObject  = new JSONObject();

try{

    // jsonObject加入属性
    jsonObject.put("name", "Que");

    jsonObject.put("age", "12");

    jsonObject.put("major", new String[] {"computer science", "infomation science"});
   jsonObject.put("real_estate",null);   

} catch (JSONException e){
  e.printStackTrace();
}

}

JSON数据的生成-使用map构建 

 

 Map<String, Object> jsonObject = new HashMap<>();

try{

    // jsonObject加入属性
    jsonObject.put("name", "Que");

    jsonObject.put("age", "12");

    jsonObject.put("major", new String[] {"computer science", "infomation science"});
   jsonObject.put("real_estate",null);   

} catch (JSONException e){ 
  e.printStackTrace(); 
} 
System.out.println(new JSONObject(jsonObject).toString());

}

 

JSON数据的生成-使用JavaBean构建 

// 根据业务需求创建java bean
// 优点:可以重用
public class User{

String name = "name';
...

String age = "age";

...

String hasAJob = "has_a_job";

}

private static void createByBean(){

  User jsonObject  = new JSONObject();

try{

    // jsonObject加入属性
    jsonObject.put("name", "Que");

    jsonObject.put("age", "12");

    jsonObject.put("major", new String[] {"computer science", "infomation science"});
   jsonObject.put("real_estate",null);   

} catch (JSONException e){ 
  e.printStackTrace(); 
} 

}

从文件中读取JSON

import org.apache.commons.io.FileUtils; 

private static void getJson() throw IOException{

//可以通过远程api读取或者通过文件内容读取

File file = new File(ReadJSONSImple.class.getResource("./data/json/xxxx.json").getFile());

String content = FileUtils.readFileToString(file);

JSONObject jsonObject = new JSONObject(content);

String name = jsonObject.getString("name");
Integer age = jsonObject.getInteger("name");

JSONArray majorArray = jsonObject.getJSONArray("major");

for (int i = 0; i < majorArray.length(); i++){

  String major = (String) majorArray.get(i);
}

}

 

从文件中读取JSON - 判断是否为null

 

jsonObject.isNull("name");

 

GSON的使用 - 服务端后台处理

GSON生成JSON文件

GSON gson = new Gson();

gson.toJson(user);

GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.setPrettyPrinting();
gsonBuilder.setFieldNamingStrategy(new FieldNamingStrategy(){

    public Strinf translateName(Field f){
        
    if (f.getName().equals("name")){
    
        return "NAME";
    }
    return f.getName();
    }
});

Gson gson = gsonBuilder.create();

//常用注解
@SeralizedName("NAME")
//name将转换为大写
private String name;

            

 

private transient String ignore;
//transient-json生成过程中忽略属性
User user = gson.fromJson(content, User.class);
// json对象反向解析

gson.toJson(user);

// json对象正向生成

//GSON可以直接集合类解析,无需再次转换

 

学习资料:https://www.imooc.com/learn/523

posted on 2022-04-04 22:44  黎酒  阅读(33)  评论(0编辑  收藏  举报