1

This is my JSON file:

"charts": {
"system_voltage": {
"enabled": true,
"title": "System Voltage",
"xaxis_label": null,
"yaxis_label": null,
"y_min": 0,
"y_max": 18,
"status": false
},
"temperature1": {
"enabled": true,
"title": "Box1 Temp",
"xaxis_label": null,
"yaxis_label": null,
"y_min": -30,
"y_max": 50,
"status": false
},
"temperature2": {
"enabled": true,
"title": "Amb Temp",
"xaxis_label": null,
"yaxis_label": null,
"y_min": -30,
"y_max": 50,
"status": true
}, ...

my original approach was doing it this way inside the for loop:

JSONObject objSysVoltage = chartss.getJSONObject("system_voltage");

but what if I have 100 of them or more?

Each object inside charts is unique. How can I loop through a JSONObject and assign each object to a JSON array without doing it manually?

JSONObject chartss = objData.getJSONObject("charts");

for(int i = 0; i < chartss.length(); i++) {
   //what goes here??  
}

2 Answers 2

3

Use keys() method to get all keys and get JSONObject form key.

Iterator<?> keys = chartss.keys();

while( keys.hasNext() ) {
  String key = (String)keys.next();
  if ( chartss.get(key) instanceof JSONObject ) {
      JSONObject josnObject = chartss.getJSONObject(key);
  }
}
Sign up to request clarification or add additional context in comments.

Comments

0

Use JsonObjectRequest and JSONArray

JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
       @Override public void onResponse(JSONObject chartss) {
           try {
               JSONArray JA = chartss.getJSONArray("charts");
               String productArray[]= new String[JA.length()];
               for (int i=0; i<JA.length();i++){
                   productArray[i]=JA.getString(i);
                   Log.d("Result",JA.getString(i));

                   ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_dropdown_item_1line, productArray);
                   t2.setAdapter(dataAdapter);
                   String T1 = t2.getSelectedItem().toString();
                   t1.setText(T1);

               }

           } catch (JSONException e) {
               e.printStackTrace();
           }
       }
   }, new Response.ErrorListener() {

       @Override public void onErrorResponse(VolleyError error) {
           error.printStackTrace();
       }
   });
   rq.add(request);}

t2 is a spinner, and t1 is textview

t2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
   @Override
   public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
       t1.setText(productArray[position]);
   }

   @Override
   public void onNothingSelected(AdapterView<?> parent) {

   }
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.