读取并动态存储JAVA中JSON中具有相同起始名称的字段。

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

reading and storing fields with same starting name dynamically from JSON in JAVA

问题

  1. private static void getData(String userJsonAsStringParam) {
  2. JSONObject jsonUserObject = new JSONObject(userJsonAsStringParam);
  3. JSONArray jsonUserArray = jsonUserObject.getJSONArray("users");
  4. for (int i = 0; i < jsonUserArray.length(); i++) {
  5. Map<String, String> map = new HashMap<String, String>();
  6. JSONObject userObject = jsonUserArray.getJSONObject(i);
  7. @SuppressWarnings("unchecked")
  8. Iterator<String> keys = userObject.keys();
  9. while (keys.hasNext()) {
  10. String key = keys.next();
  11. String value = userObject.getString(key);
  12. map.put(key, value);
  13. }
  14. }
  15. }

Error:

  1. Exception in thread "main" org.json.JSONException: JSONObject["identities"] not a string.
英文:

I have a JSON which looks like this,

  1. {
  2. &quot;users&quot;: [
  3. {
  4. &quot;displayName&quot;: &quot;Dennis Law&quot;,
  5. &quot;givenName&quot;: &quot;Dennis&quot;,
  6. &quot;surname&quot;: &quot;Law&quot;,
  7. &quot;extension_user_type&quot;: &quot;user&quot;,
  8. &quot;identities&quot;: [
  9. {
  10. &quot;signInType&quot;: &quot;emailAddress&quot;,
  11. &quot;issuerAssignedId&quot;: &quot;dennislaw@gmail.com&quot;
  12. }
  13. ],
  14. &quot;extension_timezone&quot;: &quot;VET&quot;,
  15. &quot;extension_locale&quot;: &quot;en-IN&quot;,
  16. &quot;extension_tenant&quot;: &quot;Team1&quot;
  17. },
  18. {
  19. &quot;displayName&quot;: &quot;Shaggy Nate&quot;,
  20. &quot;givenName&quot;: &quot;Shaggy&quot;,
  21. &quot;surname&quot;: &quot;Nate&quot;,
  22. &quot;extension_user_type&quot;: &quot;user&quot;,
  23. &quot;identities&quot;: [
  24. {
  25. &quot;signInType&quot;: &quot;userName&quot;,
  26. &quot;issuerAssignedId&quot;: &quot;Shaggynatealpha&quot;
  27. }
  28. ],
  29. &quot;extension_timezone&quot;: &quot;NST&quot;,
  30. &quot;extension_locale&quot;: &quot;en-AF&quot;,
  31. &quot;extension_tenant&quot;: &quot;Team1&quot;
  32. }
  33. ]
  34. }

I wrote a java code to iterate the JSON and save the values in the variable, like this, below is the content of the method() where I am iterating the JSON, so if you see, I have a wrapper users. So I took the JSON which I mentioned above and passed it as a JSON object and iterated to get all the fields like DISPLAYNAME, SURNAME....and so on.

  1. JSONObject jsonUserObject = new JSONObject(json_string);
  2. JSONArray jsonUserArray = jsonUserObject.getJSONArray(&quot;users&quot;);
  3. for (int i = 0; i &lt; jsonUserArray.length();i++) {
  4. LOG.info(&quot;Iteration...&quot;);
  5. displayName = jsonUserArray.getJSONObject(i).getString(&quot;displayName&quot;);
  6. givenName = jsonUserArray.getJSONObject(i).getString(&quot;givenName&quot;);
  7. surname = jsonUserArray.getJSONObject(i).getString(&quot;surname&quot;);
  8. extension_user_type = jsonUserArray.getJSONObject(i).getString(&quot;extension_user_type&quot;);
  9. JSONArray jsonIdentitiesArray = jsonUserArray.getJSONObject(i).getJSONArray(&quot;identities&quot;);
  10. for (int j = 0; j &lt; jsonIdentitiesArray.length();j++) {
  11. signInType = jsonIdentitiesArray.getJSONObject(j).getString(&quot;signInType&quot;);
  12. issuerAssignedId = jsonIdentitiesArray.getJSONObject(j).getString(&quot;issuerAssignedId&quot;);
  13. }
  14. try {
  15. extension_timezone = jsonUserArray.getJSONObject(i).getString(&quot;extension_timezone&quot;);
  16. extension_locale = jsonUserArray.getJSONObject(i).getString(&quot;extension_locale&quot;);
  17. extension_tenant = jsonUserArray.getJSONObject(i).getString(&quot;extension_tenant&quot;);
  18. } catch (JSONException jse) {
  19. LOG.warn(&quot;JSONException occured, some attribute was not found!&quot;, jse.getMessage());
  20. }

But I am trying to dynamically do this, if you see I have 4 fields that starts with

extension_ --> they are extension_user_type, extension_local, extension_timezone, extension_tenant,

I do not want to hard code those, I want to know a way where I do not need below line and instead read all the fields that starts with a extension_ and then store it in a varibale, dynamically,

BECAUSE the extension_ fields can be those 4 or it can be anything, i have hardcoded it, but I am looking for a way to not hardcode it and pick dynamically and store in a varibale named extension_"blabalbla"

Thanks in advance.

  1. extension_blablabla = jsonUserArray.getJSONObject(i).getString(&quot;extension_blablabla&quot;);

/////////UPDATE

  1. private static void getData(String userJsonAsStringParam) {
  2. JSONObject jsonUserObject = new JSONObject(userJsonAsStringParam);
  3. JSONArray jsonUserArray = jsonUserObject.getJSONArray(&quot;users&quot;);
  4. for (int i = 0; i &lt; jsonUserArray.length();i++) {
  5. Map&lt;String,String&gt; map = new HashMap&lt;String,String&gt;();
  6. @SuppressWarnings(&quot;unchecked&quot;)
  7. Iterator&lt;String&gt; keys = jsonUserArray.getJSONObject(i).keys();
  8. while(keys.hasNext()){
  9. String key = (String)keys.next();
  10. String value = jsonUserArray.getJSONObject(i).getString(key);
  11. map.put(key,value);
  12. }
  13. }
  14. }

Error :

  1. Exception in thread &quot;main&quot; org.json.JSONException: JSONObject[&quot;identities&quot;] not a string.

答案1

得分: 2

我认为不可能将它们动态存储在具有相同名称(例如extension_timezone)的变量中。这需要动态更改变量名称,而且您甚至不会知道哪些变量存在,哪些不存在。
绝对可能的做法是将它们存储在类似于映射(Map)的结构中:

  1. Map<String, String> map = new HashMap<String, String>();
  2. Iterator<String> keys = jsonUserArray.getJSONObject(i).keys();
  3. while(keys.hasNext()){
  4. String nextString = (String)keys.next();
  5. if(nextString.equals("identities"))
  6. {
  7. continue;
  8. }
  9. String key = nextString;
  10. String value = jsonUserArray.getJSONObject(i).getString(key);
  11. map.put(key, value);
  12. }

现在变量名称被用作映射中的键。如果您不了解映射,这里是基本信息:https://docs.oracle.com/javase/8/docs/api/java/util/Map.html

英文:

I don't think it is possible to store them dynamcly in variables that have the same name, like extension_timezone. This require to change variable names dynamicly and also you would not even know which variables exist and which not.
What is definetly possible is storing them in a Structure like a map:

  1. Map&lt;String,String&gt; map = new HashMap&lt;String,String&gt;();
  2. Iterator&lt;String&gt; keys = jsonUserArray.getJSONObject(i).keys();
  3. while(keys.hasNext()){
  4. String nextString = (String)keys.next();
  5. if(nextString.equals(&quot;identities&quot;))
  6. {
  7. continue;
  8. }
  9. String key = nextString;
  10. String value = jsonUserArray.getJSONObject(i).getString(key);
  11. map.put(key,value);
  12. }

The variable names are now used as keys in the map. If you do not know maps here are the basic infos: https://docs.oracle.com/javase/8/docs/api/java/util/Map.html

huangapple
  • 本文由 发表于 2020年10月7日 17:28:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/64241136.html
匿名

发表评论

匿名网友

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

确定