如何在Java Selenium中创建一个数组内的多个JSON对象

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

How to create a multiple JSON objects inside an Array in java selenium

问题

以下是翻译好的内容:

我希望使用一些循环以这种格式输出,因为数据包含超过20个对象。

  1. "childTargets": [
  2. {
  3. "name": "true",
  4. "rank": 86438458
  5. },
  6. {
  7. "name": "false",
  8. "rank": 86647857
  9. }
  10. ]

我不希望以这种方式写,因为代码会太长。

  1. .put(new JSONObject().put("name", "INDIA")

请帮助我。

英文:

I wanted the ouput in this format using some loop, because the data contains more than 20 objects.

  1. "childTargets": [
  2. {
  3. "name": "true",
  4. "rank": 86438458
  5. },
  6. {
  7. "name": "false",
  8. "rank": 86647857
  9. }
  10. ]

i do not want in this way to right because code will be too long

  1. .put(new JSONObject().put("name", "INDIA")

Please help me on this

答案1

得分: 1

我会创建一个包含所有姓名和一个包含所有排名的数组,然后遍历它们。

以下是一个示例:

  1. String[] names; // 所有的姓名
  2. int[] ranks; // 所有的排名,第一个排名对应第一个姓名
  3. JSONArray jsonArray = new JSONArray(); // 创建一个 JSONArray 对象。
  4. for (int i = 0; i < names.length; i++) {
  5. JSONObject jsonObject = new JSONObject();
  6. jsonObject.put("name", names[i]);
  7. jsonObject.put("rank", ranks[i]);
  8. jsonArray.add(jsonObject);
  9. }

如果你有超过两个属性,考虑使用一个包含结构体或对象的列表,而不是分散的数组。

英文:

I would craete an aarry with all names and an array all ranks and the loop through them.

Here is an example:

  1. String[] names; // all your Names
  2. int[] ranks; // all your Ranks the first rank belongs to the first name.
  3. JSONArray jsonArray = new JSONArray(); // Create an JSONArray Object.
  4. for (int i = 0; i &lt; names.length; i++) {
  5. JSONObject jsonObject = new JSONObject();
  6. jsonObject.put(&quot;name&quot;, names[i]);
  7. jsonObject.put(&quot;rank&quot;, ranks[i]);
  8. jsonArray.add(jsonObject);
  9. }

If you have more than two properties consider using one list with strucks or objects who contain all of them instead of sperad arrays.

答案2

得分: 0

You can use a library such as Gson to marshal POJO's to json cleanly.

Define a java class such as:

  1. public class ChildTargets {
  2. private List<Target> children;
  3. // getters and setters
  4. public static class Target {
  5. private String name;
  6. private String rank;
  7. // getters and setters
  8. }

Then instantiate the ChildTargets class with any Target objects you want using a loop, then call:

  1. Gson gson = new Gson();
  2. gson.toJson(childTargets);

You should get a json String in the form of:

  1. {
  2. "childTargets":[
  3. {
  4. "name":"obj1",
  5. "rank":"rank1"
  6. },
  7. {
  8. "name":"obj2",
  9. "rank":"rank2"
  10. }
  11. ]
  12. }
英文:

You can use a library such as Gson to marshal POJO's to json cleanly.

Define a java class such as:

  1. public class ChildTargets {
  2. private List&lt;Target&gt; children;
  3. // getters and setters
  4. public static class Target {
  5. private String name;
  6. private String rank;
  7. // getters and setters
  8. }

Then instantiate the ChildTargets class with any Target objects you want using a loop, then call:

  1. Gson gson = new Gson();
  2. gson.toJson(childTargets);

You should get a json String in the form of:

  1. {
  2. &quot;childTargets&quot;:[
  3. {
  4. &quot;name&quot;:&quot;obj1&quot;,
  5. &quot;rank&quot;:&quot;rank1&quot;
  6. },
  7. {
  8. &quot;name&quot;:&quot;obj2&quot;,
  9. &quot;rank&quot;:&quot;rank2&quot;
  10. }
  11. ]
  12. }

huangapple
  • 本文由 发表于 2020年8月13日 19:03:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/63393748.html
匿名

发表评论

匿名网友

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

确定