Reputation: 1108
I have a JSON File like this -
{
"users": [
{
"displayName": "Amanda Polly",
"givenName": "Amanda",
"surname": "Polly",
"extension_user_type": "user",
"identities": [
{
"signInType": "emailAddress",
"issuerAssignedId": "[email protected]"
}
],
"extension_timezone": "PST",
"extension_locale": "en-US",
"extension_tenant": "EG1234"
},
{
"displayName": "Lowa Doe",
"givenName": "Lowa",
"surname": "Doe",
"extension_user_type": "user",
"identities": [
{
"signInType": "userName",
"issuerAssignedId": "lowadow123"
}
],
"extension_timezone": "PST",
"extension_locale": "en-US",
"extension_tenant": "EG1234"
}
]
}
If you can see there are 2 users (Amanda and Lowa) wrapped inside an array "users". I parsed the file and converted all of this into a single string.
Now I am trying to iterate through all the fields like displayName, givenName, surname and so on! but if you see identities is again a wrapper for "signInType" and "issuerAssignedId".
I wrote a code that iterates through all the users but I am not able to get "identites" field. Below is my code:
Here the parameter for JSONObject is jsonAsString (which is my string that has the above JSON), now I created a JSONArray and pass the wrapper "users".
the below code is working fine but can someone please help in iterating through "identites" field for each user.
JSONObject jsonObject = new JSONObject(jsonAsString);
org.json.JSONArray jsonArray = jsonObject.getJSONArray("users");
System.out.println(jsonArray.length());
for(int i = 0; i< jsonArray.length();i++)
{
displayName = jsonArray.getJSONObject(i).getString("displayName");
givenName = jsonArray.getJSONObject(i).getString("givenName");
surname = jsonArray.getJSONObject(i).getString("surname");
extension_user_type = jsonArray.getJSONObject(i).getString("extension_user_type");
try
{
extension_timezone = jsonArray.getJSONObject(i).getString("extension_timezone");
extension_locale = jsonArray.getJSONObject(i).getString("extension_locale");
extension_tenant = jsonArray.getJSONObject(i).getString("extension_tenant");
}
catch(JSONException e)
{
System.out.println("\nSome attribute was not found!");
}
System.out.println("\ndisplayName : "+displayName);
System.out.println("\ngivenName : "+givenName);
System.out.println("\nsurname : "+surname);
System.out.println("\nextension_user_type : "+extension_user_type);
System.out.println("\nextension_timezone : "+extension_timezone);
System.out.println("\nextension_locale : "+extension_locale);
System.out.println("\nextension_tenant : "+extension_tenant);
Upvotes: 0
Views: 78
Reputation: 71
Use below code :
JSONObject jsonObject = new JSONObject(jsonAsString); org.json.JSONArray jsonArray = jsonObject.getJSONArray("users");
System.out.println(jsonArray.length());
for(int i = 0; i< jsonArray.length();i++)
{
displayName = jsonArray.getJSONObject(i).getString("displayName");
givenName = jsonArray.getJSONObject(i).getString("givenName");
surname = jsonArray.getJSONObject(i).getString("surname");
extension_user_type = jsonArray.getJSONObject(i).getString("extension_user_type");
org.json.JSONArray jsonIdentitiesArray = jsonArray.getJSONObject(i).getJSONArray("identities");
for(int j = 0; j< jsonIdentitiesArray.length();j++)
{
String signInType=jsonIdentitiesArray.getJSONObject(j).getString("signInType");
System.out.println("signInType : "+signInType);
String issuerAssignedId=jsonIdentitiesArray.getJSONObject(j).getString("issuerAssignedId");
System.out.println("issuerAssignedId : "+issuerAssignedId);
}
try
{
extension_timezone = jsonArray.getJSONObject(i).getString("extension_timezone");
extension_locale = jsonArray.getJSONObject(i).getString("extension_locale");
extension_tenant = jsonArray.getJSONObject(i).getString("extension_tenant");
}
catch(JSONException e)
{
System.out.println("\nSome attribute was not found!");
}
System.out.println("\ndisplayName : "+displayName);
System.out.println("\ngivenName : "+givenName);
System.out.println("\nsurname : "+surname);
System.out.println("\nextension_user_type : "+extension_user_type);
System.out.println("\nextension_timezone : "+extension_timezone);
System.out.println("\nextension_locale : "+extension_locale);
System.out.println("\nextension_tenant : "+extension_tenant);
Upvotes: 3
Reputation: 118
i think you can achieve that by doing the same process that you did with the user json arr, for each user use get function to get the identities json array and iterate it for each user.
JSONObject currentUserJsonObj = (JSONObject)jsonArray.getJSONObject(i);
JSONArray idnts=(jsonArray)(currentUserJsonObj.get("identities"));
Upvotes: 1