我想在Android中发送Json参数(下面) - POST方法。
{"message":"This is venkatesh","visit":[5,1,2]}
我尝试了以下代码
String IDs="5,1,2";
JSONObject jsonObject = new JSONObject();
jsonObject.put("message", "This is venkatesh");
JSONArray jsonArray = new JSONArray();
jsonArray.put(IDs);
jsonObject.put("visit", jsonArray);
String json = jsonObject.toString();
Log.d("Mainactivity", " json" + json);
我得到的输出是
{"message":"This is venkatesh","visit":["5,1,2"]}
// Output i am get with double quotes inside visit
{"message":"This is venkatesh","visit":[5,1,2]}
// I want to send this parameter without Double quotes inside the Visit
答案 0 :(得分:2)
在数组中将其添加为整数而不是字符串
JSONObject jsonObject = new JSONObject();
try {
jsonObject.put("message", "This is venkatesh");
JSONArray jsonArray = new JSONArray();
jsonArray.put(5);
jsonArray.put(1);
jsonArray.put(2);
jsonObject.put("visit", jsonArray);
String json = jsonObject.toString();
Log.i("TAG", " json" + json); //{"message":"This is venkatesh","visit":[5,1,2]}
} catch (JSONException e) {
e.printStackTrace();
}
答案 1 :(得分:2)
String IDs="5,1,2";
String[] numbers = IDs.split(",");
JSONArray jsonArray = new JSONArray();
for(int i = 0; i < numbers.length(); i++)
{
jsonArray.put(Integer.parseInt(numbers[i]));
}
希望这有帮助。
答案 2 :(得分:2)
只需更换以下行:
jsonArray.put(IDs);
使用以下代码:
jsonArray.put(5);
jsonArray.put(1);
jsonArray.put(2);
因此,如果要查看不带引号的数组,则应使用'int'值。要点是'引号'意味着这是String对象。证明是您的代码行:
String IDs="5,1,2";
答案 3 :(得分:0)
JSONObject jsonObject = new JSONObject();
jsonObject.put("message", "This is venkatesh");
JSONArray jsonArray = new JSONArray(new int[](5, 1, 2));
jsonObject.put("visit", jsonArray);
答案 4 :(得分:0)
我假设您将String转换为整数数组,然后执行此操作即可添加
您需要了解的唯一区别是,JSON
为String
而不是Integer
的值添加双引号。
因此对于String
的键值对,它将是
"key":"value"
因此对于Integer
的键值对,它将是
"key":123
因此对于boolean
的键值对,它将是
"key":true
有了这些知识,您就可以编辑代码。
代码
try {
JSONObject jsonObject = new JSONObject();
jsonObject.put("message", "This is venkatesh");
JSONArray jsonArray = new JSONArray();
jsonArray.put(0,5);
jsonArray.put(1,1);
jsonArray.put(2,2);
jsonObject.put("visit", jsonArray);
Log.d("TAG","result "+jsonObject.toString());
} catch (Exception e) {
e.printStackTrace();
}
输出
{"message":"This is venkatesh","visit":[5,1,2]}
答案 5 :(得分:0)
int[] arrayOfInteger=[1,2,3];
JSONObject jsonObject =new JSONObject();
jsonObject .put("message","your message");
JSONArray jsonArray = new JSONArray(arrayOfInteger);
jsonObject .put("visit",jsonArray );
结果:{"message":"your message","visit":[1,2,3]}