如何将JSON数组作为URL中的参数传递
我要求在Web服务调用中将一些值从移动设备传递到服务器,因此我打算以JSON格式传递所有值,如下所示
{ "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(); }
我试图通过这种方式调用上述服务
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
可以告诉我为什么我没有得到所有的价值观吗?
我建议将身体中的JSON数据作为POST
请求传递。但是如果您仍想将其作为URL中的参数传递,则必须对您的URL进行编码,例如: –
对于ex json是: – > {"name":"ABC","id":"1"}
testurl:80/service?data=%7B%22name%22%3A%22ABC%22%2C%22id%22%3A%221%22%7D
有关URL编码的更多信息,请参阅下文
我知道这可能是后来的post,但是,对于新访问者,我将分享我的解决方案,因为OP要求通过GET传递JSON对象的方法(而不是其他答案中建议的POST)。
- 获取JSON对象并将其转换为字符串(JSON.stringify)
- 取下字符串并在Base64中编码(你可以在这里找到一些有用的信息
- 将其附加到URL并进行GET调用
- 扭转过程。 解码并将其解析为一个对象
在某些我只能进行GET调用的情况下,我已经使用了它。 此外,该解决方案实际上是跨语言的。
&是下一个参数的关键字,例如ur?param1 = 1&param2 = 2
所以你有效地发送一个名为R“的第二个参数。你应该对你的字符串进行urlencode。是不是POST一个选项?
您可以通过这种方式将json Input作为POST请求和授权标头一起传递
public static JSONObject getHttpConn(String json){ JSONObject jsonObject=null; try { HttpPost httpPost=new HttpPost("http://google.com/"); org.apache.http.client.HttpClient client = HttpClientBuilder.create().build(); StringEntity stringEntity=new StringEntity("d="+json); httpPost.addHeader("content-type", "application/x-www-form-urlencoded"); String authorization="test:test@123"; String encodedAuth = "Basic " + Base64.encode(authorization.getBytes()); httpPost.addHeader("Authorization", security.get("Authorization")); httpPost.setEntity(stringEntity); HttpResponse reponse=client.execute(httpPost); InputStream inputStream=reponse.getEntity().getContent(); String jsonResponse=IOUtils.toString(inputStream); jsonObject=JSONObject.fromObject(jsonResponse); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return jsonObject; }
此方法将返回json响应。同样,您可以使用GET方法
由于@ RE350建议在post中传递JSON数据是理想的。 但是,您仍然可以将json对象作为参数发送到GET请求中,解码服务器端逻辑中的json字符串并将其用作对象。
例如,如果您使用的是php,则可以执行此操作(在其他语言中使用适当的json解码):
服务器请求:
http://?param1={"nameservice":[{"id":89},{"id":3}]}
在服务器中:
$obj = json_decode($_GET['param1'], true); $obj["nameservice"][0]["id"]
出局:
89
将Json数据字符串发送到Web地址并使用方法发布获得结果
在C#中
public string SendJsonToUrl(string Url, string StrJsonData) { if (Url == "" || StrJsonData == "") return ""; try { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url); request.Method = "POST"; request.ContentType = "application/json"; request.ContentLength = StrJsonData.Length; using (var streamWriter = new StreamWriter(request.GetRequestStream())) { streamWriter.Write(StrJsonData); streamWriter.Close(); var httpResponse = (HttpWebResponse)request.GetResponse(); using (var streamReader = new StreamReader(httpResponse.GetResponseStream())) { var result = streamReader.ReadToEnd(); return result; } } } catch (Exception exp) { throw new Exception("SendJsonToUrl", exp); } }
用PHP