Transform JSONArray into Class Object

2

I'm getting an API in my application that returns an Array of names, I've created a class to assign each name to 1 instance but I'm having trouble getting this JSONArray and turning it into object, all the methods I've tried do not Did they work, does anyone know how to do this in Java (Android)?

public void jsonToObj(JSONArray resultadoDaPesquisa) {

    try {
        JSONObject jsonObject = null;
        JSONArray jsonArray = resultadoDaPesquisa;

        jsonObject = new JSONObject(resultadoDaPesquisa.toString());
        System.out.println("resultado " + jsonObject);


    } catch (Exception e) {
        e.printStackTrace();
    }


}

org.json.JSONException: Value [{"id": 67377, "name": "Shiren the Wanderer 4: The Eye of God and the Devil's Navel"}] of type org.json.JSONArray can not be converted to JSONObject

    
asked by anonymous 18.10.2018 / 14:10

3 answers

0

This is the code I use to be JSON and transform into object:

public class JsonReader {

    static String secretKeyCaptcha = "6Ld5NXMUAAAAAOXPv-EbwycOMHfoxJrSSdLcLl-I";

    private static String readAll(Reader rd) throws IOException {
        StringBuilder sb = new StringBuilder();
        int cp;
        while ((cp = rd.read()) != -1) {
            sb.append((char) cp);
        }
        return sb.toString();
    }

    public static JSONObject readJsonFromUrl(String cep) throws IOException, JSONException {
        InputStream is = new URL("https://viacep.com.br/ws/" + cep + "/json/").openStream();
        try {
            BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
            String jsonText = readAll(rd);
            JSONObject json = new JSONObject(jsonText);
            return json;
        } finally {
            is.close();
        }
    }

    public static JSONObject validarTokenRecaptcha(String token) throws IOException {
        System.out.println("https://www.google.com/recaptcha/api/siteverify?secret=" + secretKeyCaptcha + "&response=" + token);
        InputStream is = new URL(
                "https://www.google.com/recaptcha/api/siteverify?secret=" + secretKeyCaptcha + "&response=" + token)
                        .openStream();
        try {
            BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
            String jsonText = readAll(rd);
            JSONObject json = new JSONObject(jsonText);
            return json;
        } finally {
            is.close();
        }
    }
}

Then you just have to do it:

            JSONObject jsonObj = new JSONObject();
            JsonReader jr = new JsonReader();
            jsonObj = jr.readJsonFromUrl(frete.getCepDestino());

enderecoDestino = jsonObj.get("logradouro").toString() + " - " + jsonObj.get("bairro").toString()
    
18.10.2018 / 16:36
0

Thanks for the help! I was able to do it in a different way.

    JSONObject jsonObject = null;
    try {
        for(int i = 0; i<resultadoDaPesquisa.length(); i++) {
            jsonObject = resultadoDaPesquisa.getJSONObject(i);
            String id = jsonObject.getString("id");
            String nome = jsonObject.getString("name");

            Anuncio anuncio = new Anuncio();
            anuncio.setUid(id);
            anuncio.setTitulo(nome);

            System.out.println(anuncio);
        }

    } catch (Exception e) {
        e.printStackTrace();
    }


}
    
18.10.2018 / 16:46
0

You can use the GSON library to make the job easier.

With it, from the JSON String you receive, you can create an instance of the desired class.

See an example:

Gson gson = new Gson();
SuaClasse suaClasse = gson.fromJson(stringJson, SuaClasse.class);

Here is the lib User's Guide.

    
18.10.2018 / 16:49