如何在 URL 中将 JSON 数组作为参数传递 [英] How to pass a JSON array as a parameter in URL
问题描述
我需要在 Web 服务调用中将一些值从移动设备传递到服务器,因此我计划以 JSON 格式传递所有值,如下所示
I have an requirement to pass a some values from mobile to server in a web service call and so I am planning to pass all the values in JSON format like the below
{
"nameservice": [
{
"id": 7413,
"name": "ask"
},
{
"id": 7414,
"name": "josn"
},
{
"id": 7415,
"name": "john"
},
{
"id": 7418,
"name": "R&R"
}
]
}
以下是我的服务电话
@RequestMapping("/saveName")
@ResponseBody
public String saveName(String acc)
{jsonObject = new JSONObject();
try
{
);
System.out.println(acc);
jsonObject.accumulate("result", "saved ");
}
catch(Exception e)
{
e.printStackTrace();jsonObject.accumulate("result", "Error Occured ");
}
return jsonObject.toString();
}
我正在尝试通过这种方式调用上述服务
I am trying to call the above service by this way
localhost:8080/service/saveName?acc={ "nameservice": [ { "id": 7413, "name": "ask" }, { "id": 7414, "name": "josn" }, { "id": 7415, "name": "john" }, { "id": 7418, "name": "R&R" } ] }
但是输出是这样的
{ "nameservice": [ { "id": 7413, "name": "ask" }, { "id": 7414, "name": "josn" }, { "id": 7415, "name": "john" }, { "id": 7418, "name": "R
谁能告诉我为什么我没有得到所有的值?
Can any body please tell me why I am not getting all the values please?
推荐答案
我建议将正文中的 JSON 数据作为 POST
请求传递.但如果您仍想将其作为URL 中的参数,您必须像下面这样对您的 URL 进行编码,例如:-
I would suggest to pass the JSON data in the body as a POST
request.But if you still want to pass this as a parameter in URL,you will have to encode your URL like below just for example:-
例如 json 是 :->{"name":"ABC","id":"1"}
for ex json is :->{"name":"ABC","id":"1"}
testurl:80/service?data=%7B%22name%22%3A%22ABC%22%2C%22id%22%3A%221%22%7D
有关 URL 编码的更多信息,请参阅下文
for more information on URL encoding refer below
https://en.wikipedia.org/wiki/Percent-encoding
这篇关于如何在 URL 中将 JSON 数组作为参数传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!