【问题标题】:Create json array format in android在android中创建json数组格式
【发布时间】:2017-11-17 09:28:38
【问题描述】:

我想创建 json 请求字符串。我怎样才能做到这一点?这是我的以下数组:

[1,2,3,4,5];

现在我想创建这种类型的 json 请求:

{
    "contact_approve":[
    {
    "contact_id":1    
    },
    {
    "contact_id":2    
    },
    {
    "contact_id":3    
    },
    {
    "contact_id":4    
    },
    {
    "contact_id":5    
    },
    ]

} 

有没有人知道如何创建这种类型的 json 字符串?

【问题讨论】:

  • 有一个叫做谷歌的东西,我很确定它会给你答案/方向......先试试吧
  • 创建 Json 对象并使用 for 循环将您的密钥放入其中,使用称为 Google 的东西

标签: android json arraylist


【解决方案1】:

代码如下:

  try {
        JSONObject jsonObject = new JSONObject();
        JSONArray array = new JSONArray();
        int[] dataArray = {1,2,3,4,5};
        for( int i=0;i<dataArray.length;i++) {
            JSONObject internalObject = new JSONObject();
            internalObject.put("contact_id",dataArray[i]);
            array.put(internalObject);

        }
        jsonObject.put("contact_approve", array);

        System.out.print(jsonObject);
        Log.v("JsonObject",jsonObject.toString());
    } catch (JSONException e) {
        e.printStackTrace();
    }

谢谢

【讨论】:

    【解决方案2】:

    很简单,您可以使用 Gson 库将对象转换为 json,如下所示;

    Gson gson = new Gson();
    String json = gson.toJson({your object you want to convert to json});
    

    另外要提到的是,如果您向服务器发送请求,您甚至不需要自己进行 json 序列化,使用 Retrofit,它会为您完成。

    【讨论】:

      【解决方案3】:

      遍历数组并打印每个元素的相关字符串。

      public static String ConvertToContactJson(int [] array)
      {
          String finalstr = "{\"contact_approve\":[";
      
          for (int i=0; i< array.length; i++)
          {
              finalstr += "{\"contact_id\":"+array[i]+"}";
      
              if (i < array.length-1) finalstr += ",";
          }
      
          finalstr += "]}";
      
          return finalstr;
      }
      

      【讨论】:

        【解决方案4】:

        试试这个

        try{
        
        JSONArray jarray=new JSonArray();
        
        for(int i=0;i<array.size();i++){
        
        JSONObject j=new JSONObject();
        j.put("contact_id",array[i]);
        
        jarray.put(j);
        
        }
        }catch(Exception e){
        
        e.printStackStrace();
        
        }
        

        【讨论】:

          【解决方案5】:

          JSONObject,以 {} 开头 JSONArray 以 [] 开头 在你的情况下,你有一个 JSONObject,它有一个 JSONArray。

          让我举个例子。

            public void createJson() throws JSONException {
              // Create main JSON object
              JSONObject jsonObject = new JSONObject();
          
              //Create an JSON ARRAY which will have a key "contact_approve" later on
              JSONArray jsonArray = new JSONArray();
          
              Integer contacts[] = {1,2,3,4,5};
              for (int i = 0; i < contacts.length ; i++) {
                  JSONObject contactID = new JSONObject();
                  jsonArray.put(contactID.put("contact_id", i));
              }
          
              jsonObject.put("contact_approve", jsonArray);
          
              String json = jsonObject.toString();
              Log.d("JSON", json);
          }
          

          如果您需要解析 JSON 响应,请查看 Moshi 和 GSON 库,它们会让您的生活变得更好。这些库可以将 JAVA 类解析为 JSON。 我使用 [https://codebeautify.org/jsonviewer] 查看或缩小 json。

          祝你好运。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2014-09-04
            • 1970-01-01
            • 1970-01-01
            • 2012-08-31
            • 1970-01-01
            • 1970-01-01
            • 2017-02-23
            相关资源
            最近更新 更多