0

I have a JSON with the below structure:

[
    {
        "timestamp": "2019-07-12 00:29:37",
        "OutletTemperatureCV": "31.5",
        "ChocolateMixingTemperatureCV": "30.399999",
        "WaterCoolingTemperatureCV": "13.899",
        "WaterMixingTemperatureCV": "31.5"
    }
]

The Json file is named as temperer. I am trying to parse this json array to get values values.

JSONArray array1=new JSONArray(temperer);
for (int n = 0; n < array1.length(); n++) {
    JSONObject jsonObject = array1.getJSONObject(n); 
    String time= jsonObject.getString("timestamp");
    String outletTemp= jsonObject.getString("OutletTemperatureCV");
    String outletTemp= jsonObject.getString("ChocolateMixingTemperatureCV");
    String outletTemp= jsonObject.getString("WaterCoolingTemperatureCV");
    String outletTemp= jsonObject.getString("WaterMixingTemperatureCV");
}

But when I try this code, I get the below error..

org.json.JSONException: JSONArray initial value should be a string or collection or array.

I saw another post in SO with a similar prob, but this example uses a named JSON array. (Parsing JSON Array within JSON Object)

How can I get over this error for an unnamed JSON array? Any solutions will be hugely helpful :(

5
  • What is the datatype of variable temperer? It should be of String type containing the json string. Commented Jul 17, 2019 at 8:26
  • are you reading from file? is temperature a file or variable? if variable, please share details of it Commented Jul 17, 2019 at 8:30
  • Hi, the temperer json is dynamically obtained from an API call. Structure will be the same as given here, which is an unnamed Json array if I am not wrong? Commented Jul 17, 2019 at 8:43
  • can you add some more code, including temper type @Krishna Commented Jul 17, 2019 at 8:47
  • Turns out my temperer is a String with a named array. I am not sure how to iterate over the named array now. Will post it as a separate question to avoid more confusion. Thanks for trying to help me out Commented Jul 17, 2019 at 11:36

0

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.