【问题标题】:How create a POST with JsonArray in body如何在正文中使用 JsonArray 创建 POST
【发布时间】:2017-10-03 08:00:05
【问题描述】:

我想使用 POST METHOD

将此 json 正文发送到服务器
   {
      "donatations": [
                       {
       "campaignId": "string",
       "name": "string",
       "type": 0, 
       "donationValue": 0 
       }
   ],
   "profileId": "string",
   "creditCardId": "string",
   "isPaidFees": true,
   "isRequestDonatation": true
   "amountFees": 0,
   "totalDonationAmount": 0, 
   "institutionId": "string" 
    }

我尝试了许多网络上可用的解决方案,但大多数都告诉格式化字符串并发送到服务器。是否有任何正确的方法可以将此正文发送到服务器?

我通常这样做是为了向服务器发送简单的字符串

 JsonObject dados = new JsonObject();
                dados.addProperty("profileId", sessionManager.getUsuario().getId());
                dados.addProperty("name", scanResult.cardholderName.toString());

并将 JsonObject 作为我的 POST 的正文发送

请帮助我。谢谢。

【问题讨论】:

  • 您需要从有效的 JSON 开始。此外,向我们展示您创建的不起作用的代码,解释究竟什么不起作用,也许我们可以帮助解决它。

标签: android


【解决方案1】:

您可以使用 JsonObj/Array API 创建一个:

JSONObject obj = new JSONObject();
JSONArray array = new JSONArray();
for(int i = 0; i < 10; i++){
JSONObject objFromArray = new JSONObject();
objFromArray.put("key", "value");
objFromArray.put("ke2y", "value");
array.add(objFromArray);
}
obj.put("child", array);
obj.put("normalObject", "another value");

你会有这样一个对象:

{"child": [ {/**obj*/}, /** 9 more objs*/ ], "normalObject":"anther value" }

【讨论】:

    【解决方案2】:

    正如您所提到的,我通常使用 Spring 和 Java POJO 来发送/接收数据。创建一个新的 Spring Java 应用程序。一旦你有了一个基本的 Spring 应用程序,你现在就可以用 POJO 对你的数据结构建模了。以下内容应该适用于您的数据结构:

    public class Profile() {
        private String profileId;
        private String creditCardId;
        private boolean isPaidFees;
        private List<Donations> donations;
        //etc. plus getters and setters
    }
    
    public class Donations(){
        private String campaignId;
        private String name;
        private int type;
        private float donationValue;
        //etc. plus getters and setters
    }
    

    现在您已经在 J​​ava POJO 中定义了数据结构,您可以使用 spring 创建一个 REST 接口,该接口将为您映射它。这可以在 JSon 中读取并将其转换为您的 POJO,或者它可以获取您的 POJO 并将其作为 JSon 返回。

    @RequestMapping(value = "/getProfile", method = RequestMethod.POST, consumes = "application/json", produces = "application/json")
        public Profile getProfile(@RequestBody Profile profile) {
        System.out.println(profile.profileId);
        return profile;
    }
    

    【讨论】:

      【解决方案3】:

      如果您只是创建对象来发布它,则不需要创建 pojo。这是一个非常简单的方法:

          JSONObject mainObject = new JSONObject();
          JSONArray array = new JSONArray();
          JSONObject arrayItem = new JSONObject();
      
          try {
              //PUT VALUES TO THE ARRAYITEM. 
              //YOU WILL NEED TO LOOP HERE IF YOU HAVE MORE THAN 1 ITEM IN THE JSONARRAY
              arrayItem.put("campaignId", "string");
              arrayItem.put("name", "string");
              arrayItem.put("type", 0);
              arrayItem.put("donationValue", 0);
      
              //PUT THE JSONOBJECT ITEM INSIDE THE JSONARRAY
              array.put(arrayItem);
      
              //PUT THE JSONARRAY INSIDE THE MAIN JSONOBJECT
              mainObject.put("myArray", array);
      
              //KEEP PUTTING THE REMAINING ITEMS INSIDE THE MAIN JSONOBJECT
              mainObject.put("profileId", "string");
              mainObject.put("creditCardId", "string");
              mainObject.put("isPaidFees", true);
              mainObject.put("isRequestDonatation", true);
              mainObject.put("amountFees", 0);
              mainObject.put("totalDonationAmount", 0);
              mainObject.put("institutionId", "string");
          } catch (JSONException e) {
              e.printStackTrace();
          }
      

      现在您可以像以前一样正常发送 mainObject 了。

      【讨论】:

        猜你喜欢
        • 2013-09-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-01-20
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多