How to popular a Jsonobject in hand?

Asked

Viewed 87 times

0

I’m trying but not sure, I wanted to put it in the hand created, as if I had already received my JSON file. I tried to put the JSON structure inside Jsonobject but it didn’t work.

1 answer

0


If you pass your String in JSON format to your Jsonobject object, you can do something like this:

try {
    String stringJSON = "{interests : [{interestKey:Dogs}, {interestKey:Cats}]}";
    JSONObject obj = new JSONObject(stringJSON);
    List<String> list = new ArrayList<String>();
    JSONArray array = obj.getJSONArray("interests");
    for (int i = 0; i < array.length(); i++) {
        list.add(array.getJSONObject(i).getString("interestKey"));
    }
} catch (JSONException e) {
    e.printStackTrace();
}

Source: Parsing JSON Object in Java

Browser other questions tagged

You are not signed in. Login or sign up in order to post.