java json的處理

來源:互聯網
上載者:User

標籤:

maven依賴

<dependencies>        <dependency>    <groupId>com.alibaba</groupId>    <artifactId>fastjson</artifactId>    <version>1.2.5</version></dependency>    </dependencies>

 

讀取json

class ReadJSON{    public static void main(String[] args)    {        String jsonStr = "{ \"one\":\"one\", \"two\":[{ \"two_1_1\":2.11, \"two_1_2\":2.12}, { \"two_2_1\":\"2.21\" } ], \"three\":[\"abc\",false], \"four\":{\"four_1\":4.1, \"four_2\":4.2 } }";        // one:簡單類型        // two:對象數組(最複雜)        // three:數群組類型        // four:物件類型        jsonGoogle(jsonStr);        jsonAlibaba(jsonStr);    }    // gosn讀取處理json    public static void jsonGoogle(String jsonStr)    {        JsonParser parser = new JsonParser();        JsonObject jsonObj = (JsonObject) parser.parse(jsonStr);        String one = jsonObj.get("one").getAsString();        System.out.println(one);// one        JsonArray twoObjArray = jsonObj.get("two").getAsJsonArray();        System.out.println(twoObjArray);// [{"two_1_1":2.11,"two_1_2":2.12},{"two_2_1":"2.21"}]        JsonObject twoObj = (JsonObject) twoObjArray.get(0);        String two = twoObj.get("two_1_1").getAsString();// 可以當成string處理        System.out.println(two);// 2.11        JsonArray threeArray = jsonObj.get("three").getAsJsonArray();        String three_1 = threeArray.get(0).getAsString();        boolean three_2 = threeArray.get(1).getAsBoolean();        System.out.println(three_1 + three_2);// abcfalse        JsonObject fourObj = jsonObj.get("four").getAsJsonObject();        double four_1 = fourObj.get("four_1").getAsDouble();        System.out.println(four_1);// 4.1    }    // fastjson讀取處理json    public static void jsonAlibaba(String jsonStr)    {        JSONObject jsonObj = JSON.parseObject(jsonStr);        String one = jsonObj.getString("one");        System.out.println(one);// one        JSONArray twoObjArray = jsonObj.getJSONArray("two");        System.out.println(twoObjArray);// [{"two_1_1":2.11,"two_1_2":2.12},{"two_2_1":"2.21"}]        JSONObject twoObj = twoObjArray.getJSONObject(1);        String two_2 = twoObj.getString("two_2_1");        System.out.println(two_2);// 2.21        JSONArray threeArray = jsonObj.getJSONArray("three");        String three_1 = threeArray.getString(0);        boolean three_2 = threeArray.getBoolean(1);        System.out.println(three_1 + three_2);// abcfalse        JSONObject fourObj = jsonObj.getJSONObject("four");        String four_1 = fourObj.getString("four_1");        System.out.println(four_1);// 4.1    }}

寫Json

public class Person{    private String name;    private int age;    private double salary;    private boolean hasBaby;    private List<String> babyNames;    // setter/getter/toString等}
public class WriteJSON{    public static void main(String[] args)    {        writeJsonGoogle();        writeJsonAlibaba();    }    // gson寫json    public static void writeJsonGoogle()    {        JsonObject jsonObj = new JsonObject();        jsonObj.addProperty("one", "oneStr");        jsonObj.addProperty("two", false);        JsonObject threeObj = new JsonObject();        threeObj.addProperty("three", 3);        jsonObj.add("three", threeObj);        JsonArray jsonArray = new JsonArray();        JsonObject four_1 = new JsonObject();        four_1.addProperty("four_1", 4.1);        JsonObject four_2 = new JsonObject();        four_2.addProperty("four_2", true);        jsonArray.add(four_1);        jsonArray.add(four_2);        jsonObj.add("four", jsonArray);        System.out.println(jsonObj.toString());        // {"one":"oneStr","two":false,"three":{"three":3},"four":[{"four_1":4.1},{"four_2":true}]}    }    // fastjson寫json    public static void writeJsonAlibaba()    {        Map<String, Object> jsonMap = new TreeMap<String, Object>();        jsonMap.put("one", "oneStr");        jsonMap.put("two", false);        Map<String, Object> threeObj = new HashMap<String, Object>();        threeObj.put("three_1", "3.1");        threeObj.put("three_2", 3.2);        jsonMap.put("three", threeObj);        JSONObject jsonObj = new JSONObject(jsonMap);        List<String> babynames = new ArrayList<String>();        babynames.add("abc");        babynames.add("def");        Person person = new Person("gusi", 12, 7000.00, true, babynames);        jsonObj.put("four", person);        jsonObj.put("five", 5);        System.out.println(jsonObj.toJSONString());        // {"five":5,"four":{"age":12,"babyNames":["abc","def"],"hasBaby":true,"name":"gusi","salary":7000},"one":"oneStr","three":{"three_1":"3.1","three_2":3.2},"two":false}    }}

物件類型和json的常用轉換

public class Demo implements Serializable{    String name;    int age;    boolean man;    public Demo()    {        super();    }    public Demo(String name, int age, boolean man)    {        super();        this.name = name;        this.age = age;        this.man = man;    }    // setter/getter,千萬不能忘了,不然fastjson不能設定值    @Override    public String toString()    {        return "Demo [name=" + name + ", age=" + age + ", man=" + man + "]";    }}
//gsonDemo demo1 = new Demo("a", 1, false);String json1 = new Gson().toJson(demo1);// JavaBean到StringSystem.out.println(json1);Demo demo2 = new Gson().fromJson(json1, Demo.class);// String到JavaBeanSystem.out.println(demo2);JsonObject jsonObj1 = (JsonObject) new JsonParser().parse(json1);// String到jsonObjectSystem.out.println(jsonObj1);String json2 = jsonObj1.toString();// jsonObject到StringSystem.out.println(json2);//fastjsonDemo demo3 = new Demo("b", 2, true);String json3 = JSON.toJSONString(demo3);// JavaBean到StringSystem.out.println(json3);Demo demo4 = JSON.parseObject(json3, Demo.class);// String到JavaBeanSystem.out.println(demo4);JSONObject jsonObj2 = JSON.parseObject(json3);// String到jsonObjectSystem.out.println(jsonObj2);String json4 = jsonObj2.toJSONString();// jsonObject到StringSystem.out.println(json4);JSONObject jsonObj3 = (JSONObject) JSON.toJSON(demo3);// JavaBean到jsonObjectSystem.out.println(jsonObj3);

 

java json的處理

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.