• 【Android】生成json string的四种方法



    下面介绍生成以下json数据的三种方法:

    1.利用JsonObject生成
        JsonObject jObj = new JsonObject();
    
        JsonObject jObj1 = new JsonObject();
        JsonObject jObj2 = new JsonObject();
    
        JsonObject jObj21 = new JsonObject();
        JsonObject jObj22 = new JsonObject();
    
        jObj1.addProperty("jObj11", "value11");
        jObj1.addProperty("jObj12", "value12");
    
    
        jObj21.addProperty("jObj211", "value211"); // level 2
        jObj21.addProperty("jObj212", "value212");
        jObj21.addProperty("jObj213", "value213");
    
        jObj22.addProperty("jObj221", "value221");
        jObj22.addProperty("jObj222", "value222");
        jObj22.addProperty("jObj223", "value223");
    
        jObj2.add("jObj21", jObj21);  // level 1
        jObj2.add("jObj22", jObj22);
    
        jObj.add("jObj1", jObj1); // level 0
        jObj.add("jObj2", jObj2);
    
        String json = new Gson().toJson(jObj);
    

    2.利用JSONOBJECT(org.json.JSONObject)生成

    try {
    JSONObject obj22 = new JSONObject();
    obj22.put("jobj223", "value223");
    obj22.put("jobj222", "value222");
    obj22.put("jobj221", "value221");

    JSONObject obj21 = new JSONObject();
    obj21.put("jobj213", "value213");
    obj21.put("jobj212", "value212");
    obj21.put("jobj211", "value211");

    JSONObject obj2 = new JSONObject();
    obj2.put("jobj22", obj22);
    obj2.put("jobj21", obj21);

    JSONObject obj1 = new JSONObject();
    obj1.put("jobj12", "value12");
    obj1.put("jobj11", "value11");

    JSONObject obj = new JSONObject();
    obj.put("jobj1", obj1);
    obj.put("jobj2", obj2);

    obj.toString();
    } catch (JSONException e) {
    e.printStackTrace();
    }

    3.利用Gson库生成

    首先新建下面5个需要用到的类

    1.
    public class Obj22 {

    @SerializedName("jobj223")
    private String mObj223;

    @SerializedName("jobj222")
    private String mObj222;

    @SerializedName("jobj221")
    private String mObj221;

    public Obj22(String obj223, String obj222, String obj221) {
    mObj223 = obj223;
    mObj222 = obj222;
    mObj221 = obj221;
    }

    2.

    public class Obj21 {

    @SerializedName("jobj213")
    private String mObj213;

    @SerializedName("jobj212")
    private String mObj212;

    @SerializedName("jobj211")
    private String mObj211;

    public Obj21(String obj213, String obj212, String obj211) {
    mObj213 = obj213;
    mObj212 = obj212;
    mObj211 = obj211;
    }
    }
    3.

    public class Obj2 {

    @SerializedName("jobj22")
    private Obj22 mObj22;

    @SerializedName("jobj21")
    private Obj21 mObj21;

    public Obj2(Obj22 obj22, Obj21 obj21) {
    mObj22 = obj22;
    mObj21 = obj21;
    }
    }
    4.

    public class Obj1 {

    @SerializedName("jobj12")
    private String mObj12;

    @SerializedName("jobj11")
    private String mObj11;

    public Obj1(String obj12, String obj11) {
    mObj12 = obj12;
    mObj11 = obj11;
    }
    }
    5.

    public class Obj {

    @SerializedName("jobj2")
    private Obj2 mObj2;

    @SerializedName("jobj1")
    private Obj1 mObj1;

    public Obj(Obj2 j2, Obj1 j1) {
    mObj2 = j2;
    mObj1 = j1;
    }
    }

    6.创建需要的对象

    private void objTest() {
    Obj22 j22 = new Obj22("value213", "value212", "value211");
    Obj21 j21 = new Obj21("value213", "value212", "value211");
    Obj2 j2 = new Obj2(j22, j21);

    Obj1 j1 = new Obj1("value12", "value11");
    Obj j = new Obj(j2, j1);

    }

    4.直接用string赋值

     

  • 相关阅读:
    python编程学习进度二
    python编程学习进度一
    阅读笔记(6)-《高效程序员的45个习惯》
    阅读笔记(5)-《高效程序员的45个习惯》
    阅读笔记(4)-《高效程序员的45个习惯》
    阅读笔记(3)-《高效程序员的45个习惯》
    阅读笔记(2)-《高效程序员的45个习惯》
    寒假生活15
    寒假生活14(补)
    寒假生活13
  • 原文地址:https://www.cnblogs.com/ryq2014/p/6323694.html
Copyright © 2020-2023  润新知