【问题标题】:How to create correct JSONArray in Java using JSONObject如何使用 JSONObject 在 Java 中创建正确的 JSONArray
【发布时间】:2013-09-29 18:17:31
【问题描述】:

如何使用 JSONObject 在 Java 中创建如下所示的 JSON 对象?

{
    "employees": [
        {"firstName": "John", "lastName": "Doe"}, 
        {"firstName": "Anna", "lastName": "Smith"}, 
        {"firstName": "Peter", "lastName": "Jones"}
    ],
    "manager": [
        {"firstName": "John", "lastName": "Doe"}, 
        {"firstName": "Anna", "lastName": "Smith"}, 
        {"firstName": "Peter", "lastName": "Jones"}
    ]
}

我找到了很多例子,但不是我的 JSONArray 字符串。

【问题讨论】:

    标签: java json arrays


    【解决方案1】:

    下面是一些使用 java 6 的代码来帮助您入门:

    JSONObject jo = new JSONObject();
    jo.put("firstName", "John");
    jo.put("lastName", "Doe");
    
    JSONArray ja = new JSONArray();
    ja.put(jo);
    
    JSONObject mainObj = new JSONObject();
    mainObj.put("employees", ja);
    

    编辑: 由于putadd 存在很多混淆,我将尝试解释其中的区别。在 java 6 org.json.JSONArray 中包含 put 方法,在 java 7 javax.json 中包含 add 方法。

    在 java 7 中使用构建器模式的示例如下所示:

    JsonObject jo = Json.createObjectBuilder()
      .add("employees", Json.createArrayBuilder()
        .add(Json.createObjectBuilder()
          .add("firstName", "John")
          .add("lastName", "Doe")))
      .build();
    

    【讨论】:

    • 也许也包含在 try/catch 中? (或者方法必须有 throws 语句)
    • JSONArray 没有 put 方法。
    • 用 add 代替 put
    • @PT_C 是的 JsonObject jo = Json.createObjectBuilder(); jo.add("名字", "约翰"); jo.add("姓氏", "Doe"); jo.build();
    • @ArnoldBrown 为了向 mainObj 添加一个数组,它必须有一个键。
    【解决方案2】:

    我想您是从服务器或文件中获取此 JSON,并且您想从中创建一个 JSONArray 对象。

    String strJSON = ""; // your string goes here
    JSONArray jArray = (JSONArray) new JSONTokener(strJSON).nextValue();
    // once you get the array, you may check items like
    JSONOBject jObject = jArray.getJSONObject(0);
    

    希望这会有所帮助:)

    【讨论】:

      【解决方案3】:

      可以编写可重用的小方法用于创建person json对象以避免重复代码

      JSONObject  getPerson(String firstName, String lastName){
         JSONObject person = new JSONObject();
         person .put("firstName", firstName);
         person .put("lastName", lastName);
         return person ;
      } 
      
      public JSONObject getJsonResponse(){
      
          JSONArray employees = new JSONArray();
          employees.put(getPerson("John","Doe"));
          employees.put(getPerson("Anna","Smith"));
          employees.put(getPerson("Peter","Jones"));
      
          JSONArray managers = new JSONArray();
          managers.put(getPerson("John","Doe"));
          managers.put(getPerson("Anna","Smith"));
          managers.put(getPerson("Peter","Jones"));
      
          JSONObject response= new JSONObject();
          response.put("employees", employees );
          response.put("manager", managers );
          return response;
        }
      

      【讨论】:

        【解决方案4】:

        请试试这个……希望对你有帮助

        JSONObject jsonObj1=null;
        JSONObject jsonObj2=null;
        JSONArray array=new JSONArray();
        JSONArray array2=new JSONArray();
        
        jsonObj1=new JSONObject();
        jsonObj2=new JSONObject();
        
        
        array.put(new JSONObject().put("firstName", "John").put("lastName","Doe"))
        .put(new JSONObject().put("firstName", "Anna").put("v", "Smith"))
        .put(new JSONObject().put("firstName", "Peter").put("v", "Jones"));
        
        array2.put(new JSONObject().put("firstName", "John").put("lastName","Doe"))
        .put(new JSONObject().put("firstName", "Anna").put("v", "Smith"))
        .put(new JSONObject().put("firstName", "Peter").put("v", "Jones"));
        
        jsonObj1.put("employees", array);
        jsonObj1.put("manager", array2);
        
        Response response = null;
        response = Response.status(Status.OK).entity(jsonObj1.toString()).build();
        return response;
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-05-10
          • 2014-11-24
          • 1970-01-01
          • 2012-07-20
          • 2016-01-14
          • 1970-01-01
          相关资源
          最近更新 更多