-4

I have json data format like

{
   "status":200,
   "message":"ok",
   "response": {"result":1, "time": 0.0123, "values":[1,1,0,0,0,0,0,0,0]
   }
}

I want to get one value of values array and put it on textView in eclipse. Look my code in eclipse

protected void onPostExecute (String result){
try {
JSONobject json = new JSONObject(result);
tv.setText(json.toString(1));
}catch (JSONException e){
e.printStackTrace();
}
}
  • 2
    You code seems to be missing some parts. Where is the `try` and where does the function close? – nalyd88 Sep 13 '17 at 02:18

3 Answers3

1

You can use GSON

Create a POJO for your response

public class Response{
   private int result;
   private double time;
   private ArrayList<Integer> values;

   // create SET's and GET's
}

And then use GSON to create the object you desire.

protected void onPostExecute (String result){
  try {
     Gson gson = new GsonBuilder().create();
     Response p = gson.fromJson(result, Response.class);
     tv.setText(p.getValues());
  }catch (JSONException e){
    e.printStackTrace();
  }
}
joao86
  • 2,056
  • 1
  • 21
  • 23
0

You can use jackson library for json parsing.

ObjectMapper mapper = new ObjectMapper();
Map map = mapper.readTree(json);
map.get("key");

You can use readTree if you know json is an instance of JSONObject class else use typeref and go with readValue to get the map.

pvkcse
  • 99
  • 1
  • 11
0
    protected void onPostExecute (String result){
    try {
    JSONObject json = new JSONObject(result);
    JSONObject resp = json.getJSONObject("response");
JSONArray jarr = resp.getJSONArray("values");
     tv.setText(jarr.get(0).toString(1));
    }catch (JSONException e){
     e.printStackTrace(); 
    }
     }
Francis Nduba Numbi
  • 2,499
  • 1
  • 11
  • 22