Use google gson package for Json conversion notes

Recommend teacher Mars's Android course, very suitable for introductory learning

JSON: JavaScript Object Notation (JavaScript Object Notation)

JSON is a syntax for storing and exchanging textual information. Similar to XML.

But Json is smaller when transferring data. However, the readability of Json is worse than that of xml.

The official website of json (www.json.org), but there is no need to read the entire website. If you know this website, you can go up and check it if you need it.

When using json, we can use Google's gson, and this class library is self-dependent and does not depend on other class libraries. You can use this package after installing this package, and you can search and install it yourself

When using Json, the key value must be of the String type. Why can you only use the String type? You can download it yourself. The value of value can be a valid JSON data type (string, number, object, array, boolean or null).

  • Method 1: Use gson for data analysis code actual combat:
    The following code is to manually parse the json array. Under normal circumstances, we do not use this method to parse the data. You can directly use the method two
    . The parsing steps in Jsonutil below can be divided into
    parsing arrays. Parse the object, and then take the three steps of key-value pairs
public class JSONMain {
    
    
    public static void main(String[] args) {
    
    
        String jsonArray =
                "[{\"name\":\"lll\",\"age\":24}," +
                        "{\"name\":\"hello\",\"age\":20}]";
        JsonUtil jsonUtil=new JsonUtil();
        jsonUtil.parseJsonArray(jsonArray);
        /*
        输出结果:
        name->lll
        age->24
        name->hello
        age->20
		*/
    }
}

public class JsonUtil {
    
    
    private static final String TAG = "JsonUtil";
    
    // 下面的步骤可以理解为解析数组,解析对象,然后取键值对这三步
    public void parseJsonArray(String jsonData) {
    
    
        try {
    
    
            // 首先要生成一个JsonReader对象
            JsonReader jsonReader = new JsonReader(new StringReader(jsonData));

            // 开始解析数组
            jsonReader.beginArray();
            while (jsonReader.hasNext()) {
    
    
                // 开始解析对象
                jsonReader.beginObject();
                while (jsonReader.hasNext()) {
    
    
                    // 获取标签名称
                    String tagName = jsonReader.nextName();
                    if (tagName.equals("name")) {
    
    
                    // 获取value
                        System.out.println("name->" + jsonReader.nextString());
                    } else if (tagName.equals("age")) {
    
    
                        // 获取value值
                        System.out.println("age->" + jsonReader.nextInt());
                    }
                }
                // Json对象解析完成
                jsonReader.endObject();
            }
            // Json数组解析完成
            jsonReader.endArray();
        } catch (IOException e) {
    
    
            e.printStackTrace();
        }
    }
}


  • Method 2: Convert each data object in jsonArray into a java object.
    This conversion process can be divided into two steps. The
    first step: create the java class we need according to the key value of the data object. The
    second part: convert the data in jsonArray to java object

public class JSONMain {
    
    
    public static void main(String[] args) {
    
    
        String jsonArray =
                "[{\"name\":\"lll\",\"age\":24}," +
                        "{\"name\":\"hello\",\"age\":20}]";
        Gson gson = new Gson();

        Type listType = new TypeToken<LinkedList<User>>() {
    
    }.getType();
        LinkedList<User> users = gson.fromJson(jsonArray, listType);
        for (User user : users) {
    
    
            System.out.println(user.getName());
            System.out.println(user.getAge());
        }
        /*
        lll
		24
		hello
		20
		*/
    }
}

// 根据数据类创建java类

public class User {
    
    
    private String name;
    private int age;

    public String getName() {
    
    
        return name;
    }

    public void setName(String name) {
    
    
        this.name = name;
    }

    public int getAge() {
    
    
        return age;
    }

    public void setAge(int age) {
    
    
        this.age = age;
    }
}

When we only parse a single data object, we can use the following method:

    public static void main(String[] args) {
    
    
        String jsonData =
                "{\"name\":\"lll\",\"age\":24}";
        Gson gson = new Gson();
        User user=gson.fromJson(jsonData,User.class);
        System.out.println(user.getName()+" "+user.getAge());
    }
    /*
	输出
	lll 24
	*/

Guess you like

Origin blog.csdn.net/liu_12345_liu/article/details/102991316