Here you get JSONObject so change this line:

JSONArray jsonArray = new JSONArray(readlocationFeed); 

with following:

JSONObject jsnobject = new JSONObject(readlocationFeed);

and after

JSONArray jsonArray = jsnobject.getJSONArray("locations");
for (int i = 0; i < jsonArray.length(); i++) {
    JSONObject explrObject = jsonArray.getJSONObject(i);
}
Answer from kyogs on Stack Overflow
🌐
Stack Overflow
stackoverflow.com › questions › 40948014 › how-to-convert-single-string-to-jsonarray-in-android
java - How to convert single string to JsonArray in android? - Stack Overflow
public class MainActivity extends Activity { TextView textView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView = (TextView) findViewById(R.id.lv); getCallDetails(); } private void getCallDetails() { StringBuffer sb = new StringBuffer(); String strOrder = android.provider.CallLog.Calls.DATE + " DESC"; Cursor managedCursor = managedQuery(CallLog.Calls.CONTENT_URI, null,null, null, strOrder); int number1 = managedCursor.getColumnIndex(CallLog.Calls.NUMBER); int type1 = managedCursor.getColumnIndex
🌐
Stack Overflow
stackoverflow.com › questions › 21919005 › convert-a-string-to-json-array-java
android - Convert a string to JSON array JAVA - Stack Overflow
November 30, 2016 - String json = "{\"listings\":[{\"Listing_ID\":\"1\",\"Event_ID\":\"1\",\"Venue_ID\":\"1\",\"Start_Date\":\"19\/11\/2013\",\"End_Date\":\"04\/01\/2014\",\"Frequency\":\"Every Day\"},{\"Listing_ID\":\"2\",\"Event_ID\":\"1\",\"Venue_ID\":\"4\",\"Start_Date\":\"20\/01\/2014\",\"End_Date\":\"21\/01\/2014\",\"Frequency\":\"Every 2 Da\"},{\"Listing_ID\":\"3\",\"Event_ID\":\"3\",\"Venue_ID\":\"4\",\"Start_Date\":\"22\/01\/2014\",\"End_Date\":\"23\/01\/2014\",\"Frequency\":\"Every Day\"}]}"; JSONObject object = (JSONObject) new JSONTokener(json).nextValue(); JSONArray locations = object.getJSONArray("listings");
🌐
Stack Overflow
stackoverflow.com › questions › 23256497 › convert-jsonarray-to-string
android - Convert JSONArray to String[] - Stack Overflow
// getting JSON string from URL JSONObject json = userFunction.getAudios(); List<String> list = new ArrayList<String>(); try { commentator = json.getJSONArray(TAG_COMENTATOR); Log.d("COMMENTATOR", commentator.toString()); //System.out.println("*****COMMENTATOR*****" + commentator.length()); if(commentator != null){ for (int i = 0; i < commentator.length(); i++) { Log.d("FOR LOOP", "Value: " + commentator.getString(i)); list.add( commentator.getString(i) ); } } } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } String[] titles = list.toArray(new String[list.size()]);
Find elsewhere
🌐
Stack Overflow
stackoverflow.com › questions › 17019826 › how-can-convert-string-to-json-array
android - How can convert String to Json Array - Stack Overflow
I'm getting error cannot convert String to json object .. While I'm converting this String {"user_id": "user_id: 140" }; Error getting due to the format problem? Thanks in advance..
Top answer
1 of 3
18

This is how to initialize a JSON parser:

JSONObject jsonObject = new JSONObject(jsonString);

That will give you the entire string as a Json Object. From there, pull out an individual array as a JsonArray, like this:

JSONArray jsonArray = jsonObject.getJSONArray("LotPrizes");

To access each "LotPrizes" you can use for loop logic:

for(int i=0;i<jsonArray.length();i++)
{
  JSONObject curr = jsonArray.getJSONObject(i);

  prize = curr.getString("Prize")

//Do stuff with the Prize String here
//Add it to a list, print it out, etc.
}

EDIT: Final code after your JSON edit:

JSONArray jsonArray = null;
String jsonString = <your string>
String currPrize = null;

JSONObject jsonObject = new JSONObject(jsonString);

jsonArray = jsonObject.getJSONArray("data");

for(int i=0;i<jsonArray.length();i++)
{
    JSONArray currLot = jsonArray.getJSONObject(i);

    for(int j=0; j<currLot.length();j++)
    {
        JSONobject curr = currLot.getJSONObject(j);

        currPrize = curr.getString("Prize");

        //Do something with Prize
    }
}

This code is functional and I'm using an almost identical version in my code. Hope this (finally) works for you.

2 of 3
2

You can retrieve the jsondata from your string as follows ..

            JSONObject json = new JSONObject(jsonString);
            JSONArray jData = json.getJSONArray("data");
            for (int i = 0; i < jData.length(); i++) {
                JSONObject jo = jData.getJSONObject(i);

                JSONArray jLotPrizes = jo.getJSONArray("LotPrizes");
                for (int j = 0; j < jLotPrizes.length(); j++) {
                    JSONObject jobj = jLotPrizes.getJSONObject(j);

                    Log.i("Prize", "" + jobj.getString("Prize"));
                    Log.i("Range", "" + jobj.getString("Range"));
                }

            }
🌐
Java67
java67.com › 2016 › 10 › 3-ways-to-convert-string-to-json-object-in-java.html
3 ways to convert String to JSON object in Java? Examples | Java67
jsonString = { "name" : "Ronaldo", "sport" : "soccer", "age" : 25, "id" : 121, "lastScores" : [ 2, 1, 3, 5, 0, 0, 1, 1 ] } It's simple, has 5 attributes, two of which are String and the other two are numeric. One attribute, lastScore is a JSON array. The Gson is an open-source library to deal with JSON in Java programs. It comes from none other than Google, which is also behind Guava, a common purpose library for Java programmers. You can convert JSON String to Java object in just 2 lines by using Gson as shown below :
🌐
DigitalOcean
digitalocean.com › community › tutorials › android-jsonobject-json-parsing
Android JSONObject - JSON Parsing in Android | DigitalOcean
August 4, 2022 - We’ve iterated through the JSONArray object and fetched the strings present in each child JSONObject and added them to a ArrayList that’s displayed in the ListView. The application name is changed using : ... The output of the application is given below. You can see the title name changed in the ToolBar at the top. Google has released a Volley Library for JSON Parsing. We’ll implement that in later tutorials. GSON is a Java library that converts Java Objects into JSON and vice versa.
Top answer
1 of 6
2

The problem is not the JSONArray.toString(), as @Selvin mentioned.

From JSONArray source:

/**
 * Encodes this array as a compact JSON string, such as:
 * <pre>[94043,90210]</pre>
 */
@Override public String toString() {
    try {
        JSONStringer stringer = new JSONStringer();
        writeTo(stringer);
        return stringer.toString();
    } catch (JSONException e) {
        return null;
    }
}

/**
 * Encodes this array as a human readable JSON string for debugging, such
 * as:
 * <pre>
 * [
 *     94043,
 *     90210
 * ]</pre>
 *
 * @param indentSpaces the number of spaces to indent for each level of
 *     nesting.
 */
public String toString(int indentSpaces) throws JSONException {
    JSONStringer stringer = new JSONStringer(indentSpaces);
    writeTo(stringer);
    return stringer.toString();
}

The problem is that you need to convert your ChecklistAnswer to JSON object first for your JSONArray to work properly.

Again from Javadoc:

/**
 * A dense indexed sequence of values. Values may be any mix of
 * {@link JSONObject JSONObjects}, other {@link JSONArray JSONArrays}, Strings,
 * Booleans, Integers, Longs, Doubles, {@code null} or {@link JSONObject#NULL}.
 * Values may not be {@link Double#isNaN() NaNs}, {@link Double#isInfinite()
 * infinities}, or of any type not listed here.

...

2 of 6
1

In my ChecklistAnwer class i added:

   public JSONObject toJsonObject(){
    JSONObject json = new JSONObject();

    try {
        json.put("questionId", questionId);
        json.put("checklistId", checklistId);
        json.put("answer", answer);
        json.put("remark", remark);
    } catch (JSONException e) {
        e.printStackTrace();
    }

    return json;
}

and in my other class:

JSONArray answers = new JSONArray();

ChecklistAnswer answer = new ChecklistAnswer(questions.get(id).id, 2, cb.isChecked(),         text.getText().toString());

answers.put(answer.toJsonObject());

if i filled the array:

String js = answers.toString(1);

and that returns:

[
 {
  "answer": true,
  "questionId": 1,
  "remark": "",
  "checklistId": 2
 },
 {
  "answer": false,
  "questionId": 4,
  "remark": "teesxfgtfghyfj",
  "checklistId": 2
 },
 {
  "answer": true,
  "questionId": 4,
  "remark": "",
  "checklistId": 2
 },
 {
  "answer": true,
  "questionId": 4,
  "remark": "",
  "checklistId": 2
 },
 {
  "answer": true,
  "questionId": 4,
  "remark": "",
  "checklistId": 2
 },
 {
  "answer": true,
  "questionId": 4,
  "remark": "",
  "checklistId": 2
 },
 {
  "answer": true,
  "questionId": 4,
  "remark": "",
  "checklistId": 2
 }
]

thanks to @Selvin

🌐
Android Developers
developer.android.com › api reference › jsonarray
JSONArray | API reference | Android Developers
Skip to main content · English · Deutsch · Español – América Latina · Français · Indonesia · Polski · Português – Brasil · Tiếng Việt · 中文 – 简体