java– 发送没有转义字符的嵌套JSON对象

java– 发送没有转义字符的嵌套JSON对象,第1张

概述我正在尝试使用JSONObjectRequest将嵌套的JSONObject发送到服务器.服务器期望以下列形式的JSONObject:{"commit":"SignIn","user":{"login":"myusername","password":"mypassword"}}但目前我的程序通

我正在尝试使用JSONObjectRequest将嵌套的JsONObject发送到服务器.服务器期望以下列形式的JsONObject:

{     "commit":"Sign In",   "user":{        "login":"my username",      "password":"mypassword"   }}

但目前我的程序通过以下方式发送(JsonObject.tostring())

{     "commit":"Sign In",   "user":"   {        \"login\”:\”myusername\”,      \”password\”:\”mypassword\”   }   ”}

JsONObjects由以下人员制作:

final JsONObject loginRequestJsONObject = new JsONObject();final JsONObject userjsonObject = new JsONObject();userjsonObject.put("login", "myuser");userjsonObject.put("password", "mypass");loginRequestJsONObject.put("user", userjsonObject);loginRequestJsONObject.put("commit", "Sign In");Map<String, String> paramsForjson = new HashMap<String, String>();paramsForjson.put("user", userjsonObject.toString().replaceAll("\\", "");paramsForjson.put("commit", "Sign In");JsONObject objectToSend =  new JsONObject(paramsForjson);JsonObjectRequest JsonObjectRequest = new JsonObjectRequest(Request.Method.POST, url, objectToSend,...)

如何在上面的表单中发送JsONObject?

解决方法:

这是你的错误:

paramsForjson.put("user", userjsonObject.toString().replaceAll("\\", ""));

您已将用户转换为您不需要的String,只需执行以下 *** 作:

loginRequestJsONObject.put("user", userjsonObject);

虽然你已经完成了这个,但你实际上已经有了正确的线条,这就是你所需要的:

final JsONObject loginRequestJsONObject = new JsONObject();final JsONObject userjsonObject = new JsONObject();userjsonObject.put("login", "myuser");userjsonObject.put("password", "mypass");loginRequestJsONObject.put("user", userjsonObject);loginRequestJsONObject.put("commit", "Sign In");JsONObject objectToSend = loginRequestJsONObject;
总结

以上是内存溢出为你收集整理的java – 发送没有转义字符的嵌套JSON对象全部内容,希望文章能够帮你解决java – 发送没有转义字符的嵌套JSON对象所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1117372.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存