【问题标题】:How to add a String Array in a JSON object?如何在 JSON 对象中添加字符串数组?
【发布时间】:2013-03-21 17:02:38
【问题描述】:

我正在尝试在 android 上创建这个 JSON 对象。我被困在如何在对象中添加字符串数组。

A = {
    "class" : "4" ,
    "name" : ["john", "mat", "jason", "matthew"]
    }

这是我写的代码:

import org.json.JSONObject;

JSONObject school = new JSONObject();

school.put("class","4");
school.put("name", ["john", "mat", "jason", "matthew"] );

但最后一行给出了错误。有办法过去吗?

【问题讨论】:

    标签: android json arrays


    【解决方案1】:

    您因此而遇到错误。

    school.put("name", ["john", "mat", "jason", "matthew"] );
                       ^                                 ^   
    

    这样做。

    school.put("name", new JSONArray("[\"john\", \"mat\", \"jason\", \"matthew\"]"));
    

    【讨论】:

    • @VenkateshShukla 你现在有什么问题?
    【解决方案2】:

    您收到错误,因为您的最后一行是无效的 Java。

    school.put("name", new JSONArray("[\"john\", \"mat\", \"jason\", \"matthew\"]"));
    

    【讨论】:

    • 这解决了它...实际上我必须制作一个新的 JSONArray 并将所有数据放入其中。非常感谢
    • 这是在 java 中执行此操作的错误方法,即使它解决了您当前的问题
    【解决方案3】:

    Tom 提出的一些不恰当的做法。优化后的代码是:

    ArrayList<String> list = new ArrayList<String>();
    list.add("john");
    list.add("mat");
    list.add("jason");
    list.add("matthew");
    
    JSONObject school = new JSONObject();
    
    school.put("class","4");
    school.put("name", new JSONArray(list));
    

    【讨论】:

    • 这是正确的 Java 方法。选择的答案会起作用,但不是正确的方法。
    • 要导入正确的包,你应该写: import org.json.simple.JSONArray;在java类之上。如果您使用的是 Maven,请将 com.googlecode.json-simplejson-simple1.1 添加到 pom.xml 中。 xml。然后,下载源和依赖项并重新导入。
    【解决方案4】:

    @bhavindesai 的,答案很好。这是解决此问题的另一种方法。您可以使用 Json Simple 库简单地做到这一点。这是毕业典礼

    compile 'com.googlecode.json-simple:json-simple:1.1'
    

    这里是示例代码:

    org.json.simple.JSONObject jsonObject=new org.json.simple.JSONObject();
    jsonObject.put("Object","String Object");
    
    ArrayList<String> list = new ArrayList<String>();
                list.add("john");
                list.add("mat");
                list.add("jason");
                list.add("matthew");
    
                jsonObject.put("List",list);
    

    就是这样。 :)

    【讨论】:

      【解决方案5】:

      正如 bhavindesai 所说,

      ArrayList<String> list = new ArrayList<String>();
      list.add("john");
      list.add("mat");
      list.add("jason");
      list.add("matthew");
      
      JSONObject school = new JSONObject();
      
      school.put("class","4");
      school.put("name", new JSONArray(list));
      

      是一种更好、更清洁的方法。

      要导入正确的包,您应该编写:

      import org.json.simple.JSONArray;
      

      在 java 类之上。

      如果您使用的是 Maven,请添加

       <dependency>
          <groupId>com.googlecode.json-simple</groupId>
          <artifactId>json-simple</artifactId>
          <version>1.1</version>  
      </dependency>
      

      到 pom.xml。然后,下载源和依赖项并重新导入。尽管我使用了评论,但我还是回答了,因为评论搞砸了代码缩进。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多