0

I get the request but i can't get the jsonarray from json object.

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();
}

my 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"
        }]
    }
}
Not a JD
  • 1,864
  • 6
  • 14
  • What does your JSON look like? – BeUndead May 07 '19 at 23:25
  • Possible duplicate of [How to parse JSON in Java](https://stackoverflow.com/questions/2591098/how-to-parse-json-in-java) – qtopierw May 08 '19 at 01:59
  • What does "can't get" mean exactly? Do you get an error message? If so, post it. You should also read "how to ask good questions" in the Stack Overflow help section. – Roland Illig May 08 '19 at 04:20

1 Answers1

0

Well, you can call the 'd' object first.

The sequence of your json file should be from a d(JSONObject) tag to a results(JSONArray) tag.

Here is code by using another json library

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);
        }
    }

}       
tommybee
  • 2,409
  • 1
  • 20
  • 23