Trouble parsing a json array inside a json array
First, array.get(0)
will get you the first element from the main array. This first element is a JSON object that has two properties politics
and location
. You seem to be interested in a value that is inside the array value of the politics
property. You'll have to use this ((JSONArray)((JSONObject)array.get(0)).get("politics"))
to get that array.
Second, admin4
is not a property it is actually a value of the type
property. You'll have to loop through the array to find it.
Here is a complete example:
JSONParser parser = new JSONParser();
Object obj = parser.parse(output);
JSONArray array = (JSONArray)obj;
JSONArray politics = ((JSONObject)array.get(0)).get("politics"));
JSONObject obj = null;
for(int i = 0; i < politics.size(); i++){
if(((JSONObject)politics.get(i)).getString("type").equals("admin4")){
obj = ((JSONObject)politics.get(i));
}
}
if(obj != null){
// Do something with the object.
}
It seems that you're using the simple json
library. I don't remember exactly if it is .get("politics")
or .getJSONObject("politics")
. There may be other mistakes in method names in my example.
How can I parse nested array inside JSON object using Android Volley
Create your response model class. You can do it with an online tool or android studio plugins.
You can find a good online tool here .And about android studio plugins , Go to File -> Settings -> Plugins and search for JSON to POJO and install a good plugin and restart android studio in order to use it .stop going through each and every JSON node since this is a simple JSON response. Use GSON for easy conversions. add GSON dependency to your app level Build.gradle file and start using GSON to convert your JSON response to a List of model class objects. It's simple and easy. You can find a good tutorial here.
Parse the json objects inside json Array in Android?
Maybe like this
Make necessary changes.
private void OpenparseJson(String stringJson) {
try {
JSONArray jArray = new JSONArray(stringJson);
JSONObject jObject = null;
for (int i = 0; i < jArray.length(); i++) {
jObject = jArray.getJSONObject(i);
String s1 = jObject.getString("dateticket");
String s2 = jObject.getString("description");
String s3 = jObject.getString("etat");
String s4 = jObject.getString("idticket");
String s5 = jObject.getString("nomfichier");
String s6 = jObject.getString("objet");
String s7 = jObject.getString("priorite");
}
} catch (JSONException e) {
e.printStackTrace();
}
}
How to parse 2 JSON arrays inside an object
When you do
JSONObject parentObject = new JSONObject(finalJson);
JSONObject array = parentObject.getJSONObject("result");
JSONArray parentArray = array.getJSONArray("data");
you are looking for data inside the result block. You're not going to find it there. I think you should be doing:
JSONArray parentArray = parentObject.getJSONArray("data");
**EDIT:**Just tried it out
String finalJson = "{ \n" +
"\"result\":1,\n" +
"\"data\":[ \n" +
"{ \n" +
"\"id\":\"1\",\n" +
"\"langtitude\":\"31.3256632\",\n" +
"\"latitude\":\"20.3256632\",\n" +
"\"userNumber\":\"23\",\n" +
"\"address\":\"adfsf\",\n" +
"\"userFK\":\"1\"\n" +
"},\n" +
"{ \n" +
"\"id\":\"2\",\n" +
"\"langtitude\":\"31.3256632\",\n" +
"\"latitude\":\"20.3256632\",\n" +
"\"userNumber\":\"23\",\n" +
"\"address\":\"adfsf\",\n" +
"\"userFK\":\"1\"\n" +
"}\n" +
"]\n" +
"}";
try {
JSONObject reader = new JSONObject(finalJson);
int result = reader.getInt("result");
JSONArray data = reader.getJSONArray("data");
Log.i("MYTAG", "reader:" + reader);
Log.i("MYTAG", "result: "+result);
Log.i("MYTAG", "data: "+data);
} catch (Exception e) {
e.printStackTrace();
}
logs:
reader:{"result":1,"data":[{"id":"1","langtitude":"31.3256632","latitude":"20.3256632","userNumber":"23","address":"adfsf","userFK":"1"},{"id":"2","langtitude":"31.3256632","latitude":"20.3256632","userNumber":"23","address":"adfsf","userFK":"1"}]}
result: 1
data:[{"id":"1","langtitude":"31.3256632","latitude":"20.3256632","userNumber":"23","address":"adfsf","userFK":"1"},{"id":"2","langtitude":"31.3256632","latitude":"20.3256632","userNumber":"23","address":"adfsf","userFK":"1"}]
How can I parse json array from json object?
As far as I understand you want to be able to get the key and values within the Object Items arrays, so you need to map over items array and then the key, values
from the obtained item with Object.entries()
You can do it like
if(this.state.authenticated ){
{this.GetPage.bind(this)}
this.state.stuff.Items.map((item, index) => {
Object.entries(item).forEach([key, value], () => {
console.log(key, value)
})
})
Working example
var obj = {"return":"Success",
"Items": [
{"Name":"centracore", "Type":"rollover" , "Os":"Windows", "Level":"1", "Language_Used":"Assembly", "Size":"4mb"},
{"Name":"centracore", "Type":"Atype" , "Os":"Linux" , "Level":"3", "Language_Used":"C++" , "Size":"4mb"},
{"Name":"centracore", "Type":"random" , "Os":"OSX" , "Level":"2", "Language_Used":"C" , "Size":"4mb"}
]}
obj.Items.map((item, index) => {
console.log( item);
Object.entries(item).forEach(([key, value]) => {
console.log(key, value)
})
})
JSFIDDLE
How to parse JSON Array of objects in python
Take a look at the json module. More specifically the 'Decoding JSON:' section.
import json
import requests
response = requests.get() # api call
users = json.loads(response.text)
for user in users:
print(user['id'])
Java Parse Json Array in another Json Array json.simple
This is far more easier if you use a library like GSON. However, if you wish to continue as it is, following code would extract the content within param3
. This is not a recommended way as the code would have to change if the attributes within the node gets changed.
Therefore, please try to use a JSON Parser next time
case "param3":
JSONArray jsonArray = ( JSONArray ) pair.getValue();
for( Object object : jsonArray )
{
JSONObject jsonObject = ( JSONObject ) object;
jsonObject.keySet().forEach( o ->
{
if( "param3_1".equalsIgnoreCase( o.toString() ) )
{
// extract your value here
long next = ( long ) jsonObject.get( o );
System.out.println( next );
}
else if( "param3_2".equalsIgnoreCase( o.toString() ) )
{
// extract your value here
String next = String.valueOf( jsonObject.get( o ) );
System.out.println( next );
}
} );
}
break;
Follow up this link if you wish to add GSON to this. This is a pretty easy and straight forward tutorial
Related Topics
Why Does Intellij Give Me "Package Doesn't Exist" Error
Jackson/Hibernate, Meta Get Methods and Serialization
Does Java Se 8 Have Pairs or Tuples
How to Mock New Date() in Java Using Mockito
Add Multiple Items to an Already Initialized Arraylist in Java
Difference Between an Application Server and a Servlet Container
Make Hibernate Ignore Instance Variables That Are Not Mapped
Annotation to Make a Private Method Public Only for Test Classes
Spring Session-Scoped Beans (Controllers) and References to Services, in Terms of Serialization
When Should an Illegalargumentexception Be Thrown
Jtable Getselectedrow Does Not Return the Selected Row Index
Find Duplicate Element in Array in Time O(N)
Using a Prepared Statement and Variable Bind Order by in Java with Jdbc Driver