Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass a JSON array as a parameter in URL

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"         }     ] } 

The following is my service call

@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" } ] } 

But the output is like this

{ "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?

like image 793
rocking Avatar asked Dec 20 '14 07:12

rocking


People also ask

How do you pass a JSON object into a URL in Python?

To post a JSON to the server using Python Requests Library, call the requests. post() method and pass the target URL as the first parameter and the JSON data with the json= parameter. The json= parameter takes a dictionary and automatically converts it to a JSON string.

Can we pass parameters in JSON file?

The transfer of parameters to the script can be done either directly or through a JSON or CSV file.


2 Answers

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:-

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

for more information on URL encoding refer below

https://en.wikipedia.org/wiki/Percent-encoding

like image 72
dReAmEr Avatar answered Sep 28 '22 07:09

dReAmEr


I know this could be a later post, but, for new visitors I will share my solution, as the OP was asking for a way to pass a JSON object via GET (not POST as suggested in other answers).

  1. Take the JSON object and convert it to string (JSON.stringify)
  2. Take the string and encode it in Base64 (you can find some useful info on this here
  3. Append it to the URL and make the GET call
  4. Reverse the process. decode and parse it into an object

I have used this in some cases where I only can do GET calls and it works. Also, this solution is practically cross language.

like image 40
gvelasquez85 Avatar answered Sep 28 '22 08:09

gvelasquez85