亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何形成json對象

如何形成json對象

慕少森 2023-09-20 15:13:59
我想使用 JsonObect 轉換以下 JSON,JsonArray但無法這樣做。{  "query": {    "bool": {      "must": [        {          "match": {            "customer.partnerName": "Synapse"          }        },        {          "range": {            "customer.billing.chargeAmount": {              "gte": 1,              "lte": 100            }          }        }      ],      "filter": [        {          "match": {            "customer.configId": 15          }        }      ]    }  }}我嘗試過使用JsonObject但無法達到結果。
查看完整描述

5 回答

?
繁花不似錦

TA貢獻1851條經驗 獲得超4個贊

這只是將 json 字符串簡單地復制/粘貼到 AndroidStudio 中,它會自動分割字符串并添加轉義斜杠。它看起來很糟糕,但你編寫的語法完全沒問題。


    String jsonString = " {\n" +

            "                        \"query\": {\n" +

            "                        \"bool\": {\n" +

            "                            \"must\": [\n" +

            "                                    {\"match\": \n" +

            "            { \"customer.partnerName\":   \"Synapse\"  }},\n" +

            "\n" +

            "                                    {\n" +

            "\"range\" : \n" +

            "{\n" +

            "                                        \"customer.billing.chargeAmount\" : {\n" +

            "                                            \"gte\" : 1,\n" +

            "                                            \"lte\" : 100\n" +

            "                                        }\n" +

            "                                    }}\n" +

            "                            ],\n" +

            "                            \"filter\": [\n" +

            "                                    { \"match\":  { \"customer.configId\": 15 }}\n" +

            "                            ]\n" +

            "                        }\n" +

            "                    }\n" +

            "                    }";


    // HERE BEAUTIFIED

    /*jsonString = "{\"query\":{\"bool\":{\"must\":[{\"match\":{\"customer.partnerName\":\"Synapse\"}},{\"range\":{\"customer.billing.chargeAmount\":{\"gte\":1,\"lte\":100}}}],\"filter\":[{\"match\":{\"customer.configId\":15}}]}}}";

     */


    try {

        JSONObject object = new JSONObject(jsonString);


        // NO ERRORS, OBJECT CREATED IN MY CASE

    } catch (JSONException e) {

        e.printStackTrace();

    }

您擁有的第二個選項是以編程方式創建對象、內部對象和數組..就像這樣..


    try {


        JSONObject jsonObject = new JSONObject();



        JSONObject query = new JSONObject();

        jsonObject.put("query", query);



        JSONObject bool = new JSONObject();

        query.put("bool", bool);



        JSONArray must = new JSONArray();

        bool.put("must", must);



        JSONObject matchWrap = new JSONObject();


        JSONObject match = new JSONObject();

        match.put("customer.partnerName", "Synapse");


        matchWrap.put("match", match);


        must.put(matchWrap);



        JSONObject rangeWrap = new JSONObject();


        JSONObject range = new JSONObject();


        JSONObject customerBillingChargeAmount = new JSONObject();

        customerBillingChargeAmount.put("gte", 1);

        customerBillingChargeAmount.put("lte", 100);


        range.put("customer.billing.chargeAmount", customerBillingChargeAmount);


        rangeWrap.put("range", range);



        must.put(rangeWrap);





        JSONArray filter = new JSONArray();

        bool.put("filter", filter);



        JSONObject match2Wrap = new JSONObject();


        JSONObject match2 = new JSONObject();

        match2.put("customer.configId", 15);


        match2Wrap.put("match", match2);



        filter.put(match2Wrap);




        String jsonString2 = jsonObject.toString();


        // HERE THE SAME JSON STRING AS YOUR INPUT


    } catch (JSONException e) {

        e.printStackTrace();

    }

當刪除空格、制表符、換行符等時,這會產生與輸入字符串相同的結果。


查看完整回答
反對 回復 2023-09-20
?
慕慕森

TA貢獻1856條經驗 獲得超17個贊

我認為你正在尋找的是 json 解析。這是通過以下方式完成的:


JsonParser parser = new JsonParser();

JsonObject object = (JsonObject) parser.parse(jsonData); //Insert json string data

//Do other stuff


查看完整回答
反對 回復 2023-09-20
?
萬千封印

TA貢獻1891條經驗 獲得超3個贊

所以,我想說你應該使用 JsonPath lib 來做到這一點。


? ? ? ? <dependency>

? ? ? ? ? ? <groupId>com.jayway.jsonpath</groupId>

? ? ? ? ? ? <artifactId>json-path</artifactId>

? ? ? ? ? ? <version>2.4.0</version>

? ? ? ? </dependency>

使用示例


import com.jayway.jsonpath.DocumentContext;

import com.jayway.jsonpath.JsonPath;


...


public void handle(...) {

...

? DocumentContext jsonContext = JsonPath.parse(responseBody);

? JSONArray jsonPathPreviousUrl = jsonContext.read("$..previous")

...

這將快速有效地解析您的 json。


{

? "feed": {

? ? "data": [

? ? ? {

? ? ? ? "created_time": "2017-12-12T01:24:21+0000",

? ? ? ? "message": "This picture of my grandson with Santa",

? ? ? ? "id": ""

? ? ? },

? ? ? {

? ? ? ? "created_time": "",

? ? ? ? "message": "",

? ? ? ? "id": ""

? ? ? },

? ? ? {

? ? ? ? "created_time": "",

? ? ? ? "message": "",

? ? ? ? "id": ""

? ? ? }

? ? ],

? ? "paging": {

? ? ? "previous": "https://facebook/v3.2/{your-user-id}/feed?format=json&limit=3&since=1542820440",

? ? ? "next": "https://facebook/v3.2/{your-user-id}/feed?format=json&limit=3&until=1542583212&"

? ? }

? },

? "id": "{your-user-id}"

}

}


查看完整回答
反對 回復 2023-09-20
?
白豬掌柜的

TA貢獻1893條經驗 獲得超10個贊

<script>

var txt = '{"query": {"bool": {"must": [{"match": { "customer.partnerName":   "Synapse"  }},{"range" : {                                        "customer.billing.chargeAmount" : {                                            "gte" : 1,                                            "lte" : 100                                        }                                    }}],"filter": [{ "match":  { "customer.configId": 15 }}]}}}'

var obj = JSON.parse(txt);

debugger;

document.getElementById("demo").innerHTML = obj.query;

</script>


查看完整回答
反對 回復 2023-09-20
?
互換的青春

TA貢獻1797條經驗 獲得超6個贊

您嘗試過 Google gson 嗎?這是 repo,你也可以在網上找到相關的實現。 https://github.com/google/gson


查看完整回答
反對 回復 2023-09-20
  • 5 回答
  • 0 關注
  • 218 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號