簡體   English   中英

我如何從Json對象和數組中獲取數據

[英]how i can get data from Json object and array

我得到了請求,但我無法從json對象獲取jsonarray。

JSONObject ob = JSONObject.fromObject(response.toString());

JSONArray arr = ob.getJSONArray("results");

for (int i = 0; i < arr.size(); i++) {
    JSONObject jsonAuth = arr.getJSONObject(i);
    String PREFIX = jsonAuth.get("PREFIX").toString();
}

我的json:

{
    "d": {
        "results": [{
            "__metadata": {
                "type": "ceodo.cco.loc_do.OdataV2.SecuenciasType",
                "uri": "com:443/ceodo/cco/loc_do/OdataV2.xsodata/Secuencias('d76fffbe-8c3b-4c66-bd7f-16c9d099d143')"
            },
            "SEC_ID": "d76fffbe-8c3b-4c66-bd7f-16c9d099d143",
            "PREFIX": "B",
            "TIPO_NCF": "01",
            "NUM_FROM": 50,
            "NUM_TO": 100,
            "NUM_CURRENT": 25,
            "VALID_UNTIL": "\/Date(1556582400000)\/",
            "CAJA": "b1c913fc-e319-476f-8295-64782b77de52"
        }, {
            "__metadata": {
                "type": "ceodo.cco.loc_do.OdataV2.SecuenciasType",
                "uri": "com:443/ceodo/cco/loc_do/OdataV2.xsodata/Secuencias('f3323600-6bf2-4e90-8856-56390595d748')"
            },
            "SEC_ID": "f3323600-6bf2-4e90-8856-56390595d748",
            "PREFIX": "B",
            "TIPO_NCF": "02",
            "NUM_FROM": 1,
            "NUM_TO": 100,
            "NUM_CURRENT": 35,
            "VALID_UNTIL": "\/Date(1554249600000)\/",
            "CAJA": "c90030fb-030b-4a99-929a-adc72eaf082f"
        }]
    }
}

好吧,你可以先調用'd'對象。

json文件的序列應該是從ad(JSONObject)標記到結果(JSONArray)標記。

這是使用另一個json庫的代碼

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

... skip ...

//JSONObject ob = JSONObject.fromObject(resBuf.toString());
JSONObject ob = new JSONObject(resBuf.toString());
if(ob.has("d"))
{
    JSONObject dobj = ob.getJSONObject("d");

    if(dobj.has("results"))
    {
        JSONArray arr = dobj.getJSONArray("results");

        System.out.println(arr.length());

        for (int i = 0; i < arr.length(); i++) {
            JSONObject jsonAuth = arr.getJSONObject(i);
            String PREFIX = jsonAuth.get("PREFIX").toString();
            System.out.println(PREFIX);
        }
    }

}       

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM