我想在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
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]));
}
希望这可以帮助。
只需更换以下行:
jsonArray.put(IDs);
使用以下代码:
jsonArray.put(5);
jsonArray.put(1);
jsonArray.put(2);
因此,如果要查看没有引号的数组,则应使用'int'值。要点是'引号'意味着这是String对象。证明是您的代码行:
String IDs="5,1,2";
在数组中,将其添加为整数而不是String
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();
}
JSONObject jsonObject = new JSONObject();
jsonObject.put("message", "This is venkatesh");
JSONArray jsonArray = new JSONArray(new int[](5, 1, 2));
jsonObject.put("visit", jsonArray);
我假设你将String转换为整数数组,在你这样做之后你可以添加,
那么你需要理解的唯一区别是,JSON
为String
而不是Integer
的值添加双引号。
所以对于String
的Key值对,它会是
"key":"value"
所以对于Integer
的Key值对,它会是
"key":123
所以对于boolean
的Key值对,它会是
"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]}
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]}