JAVA Processing JSONOBJECT JSONARRAY

tags: notes  java  spring

In the process of learning Java, we will inevitably encounter to process JSON data formats, and now you will introduce JSON in Java.
1, handle JSONOBJECT

public static void main(String[] args) {
         String student="{\"name\":\"lixinxin\",\"age\":\"18\",\"price\":\"29\"}";
        JSONObject json = (JSONObject) JSONObject.parse(student);
         // Know KEY, you can directly remove the data directly.
           System.out.println(json.get("name"));
           // I don't know if we can traverse data through iterators.
        Iterator iterator=json.keySet().iterator();
        while (iterator.hasNext()) {
            // Traverse the key to remove the data in Key
            String inputkey = (String)iterator.next();
              System.out.println(inputkey+"=="+json.get(inputkey));
        }
    }

2, handle JSonarray

public static void main(String[] args) {
         String student="[{\"name\":\"lixinxin\",\"age\":\"18\",\"price\":\"29\"},{\"name\":\"zhangsan\",\"age\":\"89\",\"price\":\"299\"}]";
        JSONArray jsonArray= JSON.parseArray(student);
        // for loop traversal object
        for (Object object:jsonArray) {
            // Remove the first data
            System.out.println(object.toString());
            / / Remove the JSonarray to JSONOBJEC
            JSONObject json = (JSONObject) JSONObject.parse(object.toString());
            // This can be taken directly.
            System.out.println(json.get("name"));
        }

    }

Simple and easy to understand is my pursuit

Intelligent Recommendation

JSONOBJECT / JSONARRAY

1, import dependence 2, write test class Console output:...

jsonObject,jsonArray

JSONOBJECT, JSONARRAY Dependence` JsonObject is simple to use result ``{“name”:234,“id”:123} [{“name”:234,“id”:123}]...

JSONObject、JSONArray

The JSON used before, is a key corresponding to a value, a super simple one-to-one relationship. Now useful JSON that can be nestled in layers, taking a data so cumbersome. In fact, just like the IF e...

Java converts jsonarray to jsonobject corresponding to the key value

Sometimes when we manipulate data, a lot of data is in jsonarry format. such as: Such a format is very typed in a tabular data type. But we have to take the name with id 1 so we have to change the var...

More Recommendation

Insert JSONObject in the first place in JSONArray in java

Insert JSONObject in the first place in JSONArray in java I found a lot of methods on the Internet that didn't find every JSONObject data inserted in the first place of JSONArray. then, Write one by y...

JSONObject, JSONArray and java entity class conversion

JSONObject, JSONArray and java entity class conversion Under normal circumstances: Convert JSONObject to entity class: Employee employee = JSONObject.toJavaObject(param, Employee.class); Convert JSONA...

Java parsing complex json: the use of JSONObject and JSONArray

Before the formal analysis, we need to download the jar packages needed to parse Json, a total of seven. The download address is as follows: https://download.csdn.net/download/zai_xia/10374080 You can...

About the java JSONArray JSONObject conversion problem

import lhy.client.json.JSONArray; import lhy.client.json.JSONException; import lhy.client.json.JSONObject; public class MyJson { public static void main(String[] args) throws JSONException { JSONArray...

Conversion between java object jsonObject jsonArray in fastJson

Conversion between java objects jsonObject and jsonArray in fastJson 1. Introduction Introduction: JSON (JavaScript Object Notation) is a lightweight data exchange format Two, use 1. Convert java obje...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top