This is string from jsonObject
[
{
"No": "1",
"Name": "ABC"
},
{
"No": "2",
"Name": "PQR"
},
{
"No": "3",
"Name": "XYZ"
}
]
I want convert to this string to JSONObject to get this value in JSONArray
A JSONObject constructor can be used to convert an external form JSON text into an internal form whose values can be retrieved with the get and opt methods, or to convert values into a JSON text using the put and toString methods.
JSONObject is "native" to Android SDK, JsonObject is probably the one from Gson library, the one that I use. Two different package, don't work with both ;) choose one. I had some issue with the date formatting in JSONObject.
Use this one:
import org.json.JSONArray;
// ...
String jsonStr = "[{\"No\":\"1\",\"Name\":\"ABC\"},{\"No\":\"2\",\"Name\":\"PQR\"},{\"No\":\"3\",\"Name\":\"XYZ\"}]";
JSONArray array = new JSONArray(jsonStr);
for(int i=0; i<array.length(); i++){
JSONObject jsonObj = array.getJSONObject(i);
System.out.println(jsonObj.getString("No"));
System.out.println(jsonObj.getString("Name"));
}
Output:
1
ABC
2
PQR
3
XYZ
Use Google's JSON library (google-gson):
JsonParser jsonParser = new JsonParser();
JsonElement element = jsonParser.parse(your json string);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With