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 OverflowHere 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);
}
Input String
[
{
"userName": "sandeep",
"age": 30
},
{
"userName": "vivan",
"age": 5
}
]
Simple Way to Convert String to JSON
public class Test
{
public static void main(String[] args) throws JSONException
{
String data = "[{\"userName\": \"sandeep\",\"age\":30},{\"userName\": \"vivan\",\"age\":5}] ";
JSONArray jsonArr = new JSONArray(data);
for (int i = 0; i < jsonArr.length(); i++)
{
JSONObject jsonObj = jsonArr.getJSONObject(i);
System.out.println(jsonObj);
}
}
}
Output
{"userName":"sandeep","age":30}
{"userName":"vivan","age":5}
java - How do I create a JSONArray from this string? - Stack Overflow
How do I create a JSON string from an Array?
java - Can't Convert string to JsonArray - Stack Overflow
json - Android - how to parse JsonArray from string? - Stack Overflow
The most straightforward way to convert a JSON string to a JSONArray (provided the string is valid) is like so, using your string jsonString as the parameter:
JSONArray array = new JSONArray(jsonString);
And then you can pick off the individual objects in a loop:
for (int i = 0; i < array.length(); i++) {
JSONObject object = array.getJSONObject(i);
String username = object.getString("username");
String status = object.getString("status");
}
If jsonString is your string and you like to convert it back to a List<Map<String, Object>> try with
Type type = new TypeToken<List<Map<String, Object>>>() { }.getType();
List<Map<String, Object>> listOfMap = gson.fromJson(jsonString, type);
To have a string value inside your JSON array, you must remember to backslash escape your double-quotes in your Java program. See the declaration of s below.
String s = "[[\"110917 \", 3.0099999999999998, -0.72999999999999998, 2.8500000000000001, 2.96, 685.0, 38603.0], [\"110917 \", 2.71, 0.20999999999999999, 2.8199999999999998, 2.8999999999999999, 2987.0, 33762.0]]";
Your code in the main() method works fine. Below is just a minor modification of your code in the main() method.
System.out.println("String to Json Array Stmt");
JsonParser parser = new JsonParser();
JsonElement tradeElement = parser.parse(s);
JsonArray trade = tradeElement.getAsJsonArray();
System.out.println(trade);
Lastly, remember to prefix your statement "com.google.gson.*" with the keyword "import", as shown below.
import com.google.gson.*;
I don't see the problem. This code runs fine for me:
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
public class GsonExample {
public static void main(String[] args) {
String s= "[[\"110917\", 3.0099999999999998, -0.72999999999999998," +
"2.8500000000000001, 2.96, 685.0, 38603.0], [\"110917\", 2.71," +
"0.20999999999999999, 2.8199999999999998, 2.8999999999999999," +
"2987.0, 33762.0]]";
JsonParser parser = new JsonParser();
JsonElement elem = parser.parse( s );
JsonArray elemArr = elem.getAsJsonArray();
System.out.println( elemArr );
}
}
The only problem maybe is that you failed to properly escape the double quotes in your s string literal.
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.
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"));
}
}
I'll elaborate a bit more on ChrisR awesome answer and bring images from his awesome reference.
A valid JSON always starts with either curly braces { or square brackets [, nothing else.
{ will start an object:

{ "key": value, "another key": value }
Hint: although javascript accepts single quotes
', JSON only takes double ones".
[ will start an array:

[value, value]
Hint: spaces among elements are always ignored by any JSON parser.
And value is an object, array, string, number, bool or null:

So yeah, ["a", "b"] is a perfectly valid JSON, like you could try on the link Manish pointed.
Here are a few extra valid JSON examples, one per block:
{}
[0]
{"__comment": "json doesn't accept comments and you should not be commenting even in this way", "avoid!": "also, never add more than one key per line, like this"}
[{ "why":null} ]
{
"not true": [0, false],
"true": true,
"not null": [0, 1, false, true, {
"obj": null
}, "a string"]
}
Your JSON object in this case is a list. JSON is almost always an object with attributes; a set of one or more key:value pairs, so you most likely see a dictionary:
{ "MyStringArray" : ["somestring1", "somestring2"] }
then you can ask for the value of "MyStringArray" and you would get back a list of two strings, "somestring1" and "somestring2".
Here is some code using java 6 to get you started:
JSONObject jo = new JSONObject();
jo.put("firstName", "John");
jo.put("lastName", "Doe");
JSONArray ja = new JSONArray();
ja.put(jo);
JSONObject mainObj = new JSONObject();
mainObj.put("employees", ja);
Edit: Since there has been a lot of confusion about put vs add here I will attempt to explain the difference. In java 6 org.json.JSONArray contains the put method and in java 7 javax.json contains the add method.
An example of this using the builder pattern in java 7 looks something like this:
JsonObject jo = Json.createObjectBuilder()
.add("employees", Json.createArrayBuilder()
.add(Json.createObjectBuilder()
.add("firstName", "John")
.add("lastName", "Doe")))
.build();
I suppose you're getting this JSON from a server or a file, and you want to create a JSONArray object out of it.
String strJSON = ""; // your string goes here
JSONArray jArray = (JSONArray) new JSONTokener(strJSON).nextValue();
// once you get the array, you may check items like
JSONOBject jObject = jArray.getJSONObject(0);
Hope this helps :)