-1

I have a Json script which get data from a Mysql database and print it in different TextViews. The thing I want to know is how can I take for example only the "idE" value and print it in the "TextViewTitulo" TextView

try {
String response = "[{"0":"1","id":"1","1":"f3n","idE":"f3n","2":"bar","tipo":"bar"},{"0":"1","id":"1","1":"f3n","idE":"f3n","2":"bar2","tipo":"bar2"}]"
JSONArray array;
array = new JSONArray(response);


StringBuilder sb = new StringBuilder();
for (i=0; i<array.length(); i++) {
    // chain each string, separated with a new line
    sb.append(array.getString(i) + "\n");
}
// display the content on textview
textViewTitulo.setText(sb.toString());

} catch (JSONException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

My code works fine, but is displays the whole string of values

Yasin Kaçmaz
  • 6,573
  • 5
  • 40
  • 58
Gustavo Serna
  • 117
  • 1
  • 12
  • Please specify what exactly you want to print out. `sb.append(array.getString(i) + "\n");` prints whole JSON object. – egoldx Jun 29 '16 at 22:01

2 Answers2

0

Of course you can get it, you just have to iterate through the whole JSONArray. If you know the value you are looking for, you have to search for it, if you know the index - it is easier, you just have to get it. So for the first case, you could do something like:

for (int i = 0; i < array.length(); i++) {
  JSONObject current = array.getJSONObject(i);
  // In your case the JSONObject is more complicated
  // so you need to iterate over it too
  Iterator<?> keys = current.keys();

  while(keys.hasNext()) {
      String key = (String)keys.next();
      String value = current.get(key);
      // Do something with the value
  }

  // Or If you know what key you are looking for, you may do
  String value = current.get("idE");
  // Do something with value
}
victor175
  • 624
  • 3
  • 10
  • 23
0

You have 2 objects in your JSONArray, and 1 textview to show the data in, so I'm adding append " " to separate them.

 for (i=0; i<array.length(); i++) {
        sb.append(array.getJSONObject(i).getString("idE"));
        sb.append(" ");
        //do whatever with it
    }
 textViewTitulo.setText(sb.toString());
Vucko
  • 7,371
  • 2
  • 27
  • 45
  • that worked, but lets say I have an array with "x" number of textviews that generate depending on the number of values. How do I do so one value of idE prints in one textView and the other value in another textView and so on? – Gustavo Serna Jun 29 '16 at 22:20
  • Well you need to create an appropriate amount of textview's programmatically or use a listView and populate each of the items with a proper text. Please consider accepting the answer if it solved your original issue. – Vucko Jun 29 '16 at 22:31
  • I already have the appropiate amount of textviews, the problem is that all the "ide's" place in the same textview, do you understand me? – Gustavo Serna Jun 29 '16 at 22:34
  • Then either manually assign the text for each textView without using the loop (hardcoding way), or add all the textviews to a List and then in the loop get each and set text for each. – Vucko Jun 29 '16 at 22:53