JAVA parse json -------- json-simple

2019-10-29   18:53:13

Dian a Glance

  json-simp is a convenient process json java there is provided a decoding or encoding function json

Two Dian function

  The use of lightweight library to decode / JSON parsing and text conversion

  Flexible, the interface is simple and easy to be reused Map and List;

   . JSON supports streaming of the text output;

   High performance;
   . Does not depend on other libraries;
   All files and execute code and compatible JDK 1.2

   Support json array decoding and encoding

Wed and use

  1. Create a java project

      Import in the project json-simple jar package

      Right-engineering the election build path

      Then configure ....

      Or directly introduced maven

        <dependency>
          <groupId>com.googlecode.json-simple</groupId>
          <artifactId>json-simple</artifactId>
          <version>1.1.1</version>
        </dependency>

  2. Parse the json data

    Ordinary json parsing  

package com.lxl.learn.json;

import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;

public class json {

    public static void main(String[] args) throws Exception {
        String test_json = "{\"temp\":\"27.9度\",\"city\":\"北京\",\"weather\":\"多云转阴\",\"WS\":\"小于3级\",\"temp2\":\"31℃\",\"WD\":\"南风\",\"temp1\":\"18℃\"}";
        JSONObject json = (JSONObject) new JSONParser().parse(test_json);
        System.out.println("城市:"+json.get("city").toString());
        System.out.println("Temperature:" + json.get ( "TEMP" ) .toString ());
         / *   Output 
         * City: Beijing 
         * Temperature: 27.9 degrees 
         * / 
    } 

}

    . Json array resolved

package com.lxl.learn.json;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;

public class json {

    public static void main(String[] args) throws Exception {
        String data = ""+
                "[\r\n" + 
                "{\"city\":\"北京\",\"temp\":\"27.9\",\"WD\":\"南风\",\"WS\":\"小于3级\",\"temp1\":\"18℃\",\"temp2\":\"31℃\",\"weather\":\"多云转阴\"},\r\n" + 
                "{\ "City \": \ "Guangzhou \", \ "temp \": \ "26.6 \", \ "WD \": \ "southeast wind \", \ "WS \": \ "less than grade 3 \ ", \" temp1 \ ": \" 26 ℃ \ ", \" temp2 \ ": \" 29 ℃ \ ", \" weather \ ": \" heavy rain showers changing \ "}, \ r \ n" +
                "{\" city \ ": \" Guangyuan \ ", \" temp \ " : \" 24.8 \ ", \" WD \ ": \" north \ ", \" WS \ " : \" less than grade 3 \ ", \" temp1 \ " : \" 16 ℃ \ ", \" temp2 \ ": \" 28 ℃ \ ", \" weather \ ": \" overcast to cloudy \ "}, \ r \ n " + 
                "{\" city \ ": \" Florida \ ", \" temp \ " : \" 20.2 \ ", \" WD \ ": \" southerly \ ", \" WS \ " : \" level less than 3 \ ", \" temp1 \ " : \" 17 ℃ \ ", \" temp2 \ ": \" 27 ℃ \ ", \" weather \ ": \" showers cloudy \ "} \ R & lt \ n-" + 
                " ] " ;
         // JSON array parsing 
        the JSONArray dataArr = (the JSONArray) new new JSONParser () the parse (Data);.
         // get the array index 0,The data obtained here can be converted directly to the JSONObject strong (hereby referred to click) 
        the JSONObject FirstData = (the JSONObject) dataArr.get (0 ); 
        System.out.println ( "City:" + firstData.get ( "City" ) .toString ( )); 
        System.out.println ("Temperature:" + firstData.get ( "TEMP" ) .toString ());
         / *   Output 
         * City: Beijing 
         * Temperature: 27.9 
         * / 
    } 

}

    .json multi-level resolution, (similar to the more stages)

package com.lxl.learn.json;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;

public class json {

    public static void main(String[] args) throws Exception {
        String data = "{\"data\":[\r\n" + 
                "{\"city\":\"北京\",\"temp\":\"27.9\",\"WD\":\"南风\",\"WS\":\"小于3级\",\"temp1\":\"18℃\",\"temp2\":\"31℃\",\"weather\":\"多云转阴\"},\r\n" + 
                "{\"city\":\"Guangzhou \ ", \" temp \ ": \" 26.6 \ ", \" WD \ ": \" southeast wind \ ", \" WS \ ": \" less than grade 3 \ ", \" temp1 \ ": \ "26 ℃ \", \ " temp2 \": \ "29 ℃ \", \ "weather \": \ " heavy rain showers changing \"} \ R & lt \ n-"+ 
                "]} ";
         // convert JSON 
        the JSONObject json_data = (the JSONObject) new new JSONParser () the parse (data);.
         // obtained data array 
        the JSONArray dataArr = (the JSONArray) json_data.get ( "data" );
         // get the array index 0 
        the JSONObject = FirstData (the JSONObject) dataArr.get (0 ); 
        System.out.println ( "city:" + firstData.get ( "city" ) .toString ()); 
        System.out.println ( "temperature:" + firstData. GET ( "the TEMP" ) .toString ());
         / *   output 
         * city: Beijing 
         * temperature: 27.9 
         * / 
    } 

}

  3. encoded data

    The configuration of ordinary json

package com.lxl.learn.json;

import org.json.simple.JSONObject;

public class json {

    public static void main(String[] args) throws Exception {
        JSONObject json = new JSONObject();
        json.put("city", "北京");
        json.put("temp", "27.9度");
        System.out.println(json.toJSONString());
      /*
      * 输出: {"temp":"27.9度","city":"北京"}
      *
      */
} }

      . Building Arrays json

package com.lxl.learn.json;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

public class json {

    public static void main(String[] args) throws Exception {
        JSONArray dataArr = new JSONArray();
        //第一项数据
        JSONObject json1 = new JSONObject();
        json1.put("city", "北京");
        json1.put("temp", "27.9度");
        dataArr.add(json1);
        
        JSONObject json2 = new JSONObject();
        json2.put("city", "广州");
        json2.put("temp", "19.5度");
        dataArr.add(json2);
        
        System.out.println(dataArr.toJSONString());
        /*
         * 输出
         * [{"temp":"27.9度","city":"北京"},{"temp":"19.5度","city":"广州"}]
         */
    }

}

     Use Map construction json

package com.lxl.learn.json;

import java.util.LinkedHashMap;
import java.util.Map;

import org.json.simple.JSONValue;

public class json {

    public static void main(String[] args) throws Exception {
        Map json1 = new LinkedHashMap();
        json1.put("city", "北京");
        json1.put("temp", "27.9度");
        json1.put("temp1", "13度");
        json1.put("temp2", "30度");
        String data =  new JSONValue().toJSONString(json1);
        System.out.println(data);
        /*
         * 输出
         * {"city":"北京","temp":"27.9度","temp1":"13","temp2":"30度"}
         */
    }

}

    Using List json array structure

package com.lxl.learn.json;

import java.util.LinkedList;
import java.util.List;

import org.json.simple.JSONObject;
import org.json.simple.JSONValue;

public class json {

    public static void main(String[] args) throws Exception {
        
        //这里使用map构造普通json也一样
        
        JSONObject obj1 = new JSONObject();
        obj1.put("city", "北京");
        obj1.put("temp", "29.7");
        
        JSONObject obj2 = new JSONObject();
        obj2.put("city", "广州");
        obj2.put("temp", "19");
        
        List li = new LinkedList();
        li.add(obj1.toJSONString());
        li.add(obj2.toJSONString());
        
        String json = new JSONValue().toJSONString(li);
        System.out.println(json);
        
        /*
         * 输出
         * ["{\"temp\":\"29.7\",\"city\":\"北京\"}","{\"temp\":\"19\",\"city\":\"广州\"}"]
         */
    }

}

Four Dian Other

  Simply use the json, there is also an array or merge json json. putall method can be invoked separately

Guess you like

Origin www.cnblogs.com/lxlw/p/11761042.html