How to send json data in url as request parameters

2019-01-20 06:50发布

I want to send json data in url as below .

editTest.jsp?details=374889331-{"aNumber":2}

How can I do this?

4条回答
成全新的幸福
2楼-- · 2019-01-20 07:29

URL encode your details parameter:

String otherParameter = "374889331-";    
String jsonString = "{\"aNumber\":2}";

String url = "editTest.jsp?details=" + URLEncoder.encode(otherParameter + jsonString, "UTF-8");
查看更多
看我几分像从前
3楼-- · 2019-01-20 07:32

We can use the help of Gson

String result =new Gson().toJson("your data");

NB: jar file needed for Gson

查看更多
Explosion°爆炸
4楼-- · 2019-01-20 07:40

Json2 can help. JSON.stringify(obj)

查看更多
我想做一个坏孩纸
5楼-- · 2019-01-20 07:44

you need to convert the JSON object to string

      JSONObject obj = new JSONObject();

      obj.put("name","foo");

      StringWriter out = new StringWriter();
      obj.writeJSONString(out);

      String jsonText = out.toString();//JSON object is converted to string

Now, you can pass this jsonText as parameter.

查看更多
登录 后发表回答