解析JSON数组为对象(嵌套JSON)

huangapple go评论74阅读模式
英文:

Parsing a JSON array into Object (Nested JSON)

问题

以下是翻译好的部分:

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

// ... 其他代码 ...

JSONObject jsonObject = new JSONObject(jsonAsString);
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");

        JSONArray identitiesArray = jsonArray.getJSONObject(i).getJSONArray("identities");
        for (int j = 0; j < identitiesArray.length(); j++) {
            JSONObject identity = identitiesArray.getJSONObject(j);
            signInType = identity.getString("signInType");
            issuerAssignedId = identity.getString("issuerAssignedId");
            System.out.println("\nsignInType : " + signInType);
            System.out.println("issuerAssignedId : " + issuerAssignedId);
        }
    } catch (JSONException e) {
        System.out.println("\nSome attribute was not found!");
    }

    System.out.println("\ndisplayName : " + displayName);
    System.out.println("givenName : " + givenName);
    System.out.println("surname : " + surname);
    System.out.println("extension_user_type : " + extension_user_type);
    System.out.println("extension_timezone : " + extension_timezone);
    System.out.println("extension_locale : " + extension_locale);
    System.out.println("extension_tenant : " + extension_tenant);
}

请注意,我已经在代码中添加了处理"identities"字段的部分,以便遍历并获取"signInType"和"issuerAssignedId"。

英文:

I have a JSON File like this -

{
&quot;users&quot;: [
{
&quot;displayName&quot;: &quot;Amanda Polly&quot;,
&quot;givenName&quot;: &quot;Amanda&quot;,
&quot;surname&quot;: &quot;Polly&quot;,
&quot;extension_user_type&quot;: &quot;user&quot;,
&quot;identities&quot;: [
{
&quot;signInType&quot;: &quot;emailAddress&quot;,
&quot;issuerAssignedId&quot;: &quot;amandapolly@gmail.com&quot;
}
],
&quot;extension_timezone&quot;: &quot;PST&quot;,
&quot;extension_locale&quot;: &quot;en-US&quot;,
&quot;extension_tenant&quot;: &quot;EG1234&quot;
},
{
&quot;displayName&quot;: &quot;Lowa Doe&quot;,
&quot;givenName&quot;: &quot;Lowa&quot;,
&quot;surname&quot;: &quot;Doe&quot;,
&quot;extension_user_type&quot;: &quot;user&quot;,
&quot;identities&quot;: [
{
&quot;signInType&quot;: &quot;userName&quot;,
&quot;issuerAssignedId&quot;: &quot;lowadow123&quot;
}
],
&quot;extension_timezone&quot;: &quot;PST&quot;,
&quot;extension_locale&quot;: &quot;en-US&quot;,
&quot;extension_tenant&quot;: &quot;EG1234&quot;
}
]
}

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(&quot;users&quot;);
System.out.println(jsonArray.length());
for(int i = 0; i&lt; jsonArray.length();i++)
{
displayName = jsonArray.getJSONObject(i).getString(&quot;displayName&quot;);
givenName = jsonArray.getJSONObject(i).getString(&quot;givenName&quot;);
surname = jsonArray.getJSONObject(i).getString(&quot;surname&quot;);
extension_user_type = jsonArray.getJSONObject(i).getString(&quot;extension_user_type&quot;);
try
{
extension_timezone = jsonArray.getJSONObject(i).getString(&quot;extension_timezone&quot;);
extension_locale = jsonArray.getJSONObject(i).getString(&quot;extension_locale&quot;);
extension_tenant = jsonArray.getJSONObject(i).getString(&quot;extension_tenant&quot;);
}
catch(JSONException e)
{
System.out.println(&quot;\nSome attribute was not found!&quot;);
}
System.out.println(&quot;\ndisplayName : &quot;+displayName);
System.out.println(&quot;\ngivenName : &quot;+givenName);
System.out.println(&quot;\nsurname : &quot;+surname);
System.out.println(&quot;\nextension_user_type : &quot;+extension_user_type);
System.out.println(&quot;\nextension_timezone : &quot;+extension_timezone);
System.out.println(&quot;\nextension_locale : &quot;+extension_locale);
System.out.println(&quot;\nextension_tenant : &quot;+extension_tenant);

答案1

得分: 3

// 使用以下代码:

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("givenName : " + givenName);
    System.out.println("surname : " + surname);
    System.out.println("extension_user_type : " + extension_user_type);
    System.out.println("extension_timezone : " + extension_timezone);
    System.out.println("extension_locale : " + extension_locale);
    System.out.println("extension_tenant : " + extension_tenant);
}
英文:

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&lt; jsonArray.length();i++)
{
displayName = jsonArray.getJSONObject(i).getString(&quot;displayName&quot;);
givenName = jsonArray.getJSONObject(i).getString(&quot;givenName&quot;);
surname = jsonArray.getJSONObject(i).getString(&quot;surname&quot;);
extension_user_type = jsonArray.getJSONObject(i).getString(&quot;extension_user_type&quot;);
org.json.JSONArray jsonIdentitiesArray = jsonArray.getJSONObject(i).getJSONArray(&quot;identities&quot;);
for(int j = 0; j&lt; jsonIdentitiesArray.length();j++)
{
String signInType=jsonIdentitiesArray.getJSONObject(j).getString(&quot;signInType&quot;);
System.out.println(&quot;signInType : &quot;+signInType);
String issuerAssignedId=jsonIdentitiesArray.getJSONObject(j).getString(&quot;issuerAssignedId&quot;);
System.out.println(&quot;issuerAssignedId : &quot;+issuerAssignedId);
}
try
{
extension_timezone = jsonArray.getJSONObject(i).getString(&quot;extension_timezone&quot;);
extension_locale = jsonArray.getJSONObject(i).getString(&quot;extension_locale&quot;);
extension_tenant = jsonArray.getJSONObject(i).getString(&quot;extension_tenant&quot;);
}
catch(JSONException e)
{
System.out.println(&quot;\nSome attribute was not found!&quot;);
}
System.out.println(&quot;\ndisplayName : &quot;+displayName);
System.out.println(&quot;\ngivenName : &quot;+givenName);
System.out.println(&quot;\nsurname : &quot;+surname);
System.out.println(&quot;\nextension_user_type : &quot;+extension_user_type);
System.out.println(&quot;\nextension_timezone : &quot;+extension_timezone);
System.out.println(&quot;\nextension_locale : &quot;+extension_locale);
System.out.println(&quot;\nextension_tenant : &quot;+extension_tenant);

答案2

得分: 1

我认为你可以通过执行与用户JSON数组相同的过程来实现,针对每个用户使用get函数来获取身份识别信息的JSON数组,并对每个用户进行迭代。

JSONObject currentUserJsonObj = (JSONObject) jsonArray.getJSONObject(i);

JSONArray idnts = (JSONArray) currentUserJsonObj.get("identities");
英文:

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"));

huangapple
  • 本文由 发表于 2020年8月24日 22:07:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/63562709.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定