JAVA解析json--------json-simple


2019-10-29   18:53:13

一丶概覽

  json-simp是一個方便java的json處理,提供了解碼或編碼json的功能

二丶功能

  . 使用輕量級的庫來解碼/解析和轉換JSON文本

  . 靈活,簡單並且易於被Map和List接口重用;

   . 支持流式的JSON文本輸出;

   . 高性能;
   . 不依賴其它的庫;
   . 所有的代碼和執行文件都和JDK 1.2兼容

   .支持json數組的解碼和編碼

三丶使用

  1.創建一個java工程

      .在工程中導入json-simple的jar包

      .右鍵工程選build path

      然后點configure....

      .或者直接使用maven導入

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

  2.解析json數據

    . 普通json解析  

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("溫度:"+json.get("temp").toString());
        /*  輸出
         *  城市:北京
         *  溫度:27.9度
         */
    }

}

    . json數組解析

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\":\"廣州\",\"temp\":\"26.6\",\"WD\":\"東南風\",\"WS\":\"小於3級\",\"temp1\":\"26℃\",\"temp2\":\"29℃\",\"weather\":\"陣雨轉暴雨\"},\r\n" + 
                "{\"city\":\"廣元\",\"temp\":\"24.8\",\"WD\":\"北風\",\"WS\":\"小於3級\",\"temp1\":\"16℃\",\"temp2\":\"28℃\",\"weather\":\"陰轉多雲\"},\r\n" + 
                "{\"city\":\"達州\",\"temp\":\"20.2\",\"WD\":\"南風\",\"WS\":\"小於3級\",\"temp1\":\"17℃\",\"temp2\":\"27℃\",\"weather\":\"陣雨轉多雲\"}\r\n" + 
                "]";
        //json解析數組
        JSONArray dataArr = (JSONArray) new JSONParser().parse(data);
        //得到數組下標0,這里得到的數據可以直接強轉換為JSONObject(特此記一下)
        JSONObject firstData = (JSONObject) dataArr.get(0);
        System.out.println("城市:"+firstData.get("city").toString());
        System.out.println("溫度:"+firstData.get("temp").toString());
        /*  輸出
         *  城市:北京
         *  溫度:27.9
         */
    }

}

    .json的多級解析,(更多級類似)

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\":\"廣州\",\"temp\":\"26.6\",\"WD\":\"東南風\",\"WS\":\"小於3級\",\"temp1\":\"26℃\",\"temp2\":\"29℃\",\"weather\":\"陣雨轉暴雨\"}\r\n" + 
                "]}";
        //轉換json
        JSONObject json_data = (JSONObject) new JSONParser().parse(data);
        //得到data的數組
        JSONArray dataArr = (JSONArray) json_data.get("data");
        //得到數組下標0
        JSONObject firstData = (JSONObject) dataArr.get(0);
        System.out.println("城市:"+firstData.get("city").toString());
        System.out.println("溫度:"+firstData.get("temp").toString());
        /*  輸出
         *  城市:北京
         *  溫度:27.9
         */
    }

}

  3.編碼數據

    .構造普通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":"北京"}
      *
      */
} }

      .構造數組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":"廣州"}]
         */
    }

}

     .使用Map構造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度"}
         */
    }

}

    .使用List構造json數組

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\":\"廣州\"}"]
         */
    }

}

四丶其他

  只是對json的簡單使用,里面還有合並json數組或者json。分別調用putall方法即可


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM