【问题标题】:JSON httprequest syntaxJSON http请求语法
【发布时间】:2013-08-22 01:22:24
【问题描述】:

我有一个 JSON 函数,它调用一个 url 并在提示消息中显示它的内容,这个函数工作我用这个 url 测试了它:http://date.jsontest.com/

但我必须做一个包含此内容的 url://这是一个 java 文件一个类

reponse_tex="{ id:" + p_id + ",Categorie: "+ p_demandAide.getP_categorie()+ ",Type: " +p_demandAide.getP_type() +",Message: " +p_demandAide.getP_message()+",Reponse: " +p_demandAide.getP_response() + " }";

我的课程有效,它向我展示了浏览器中的内容,但我知道我必须以某种格式编写它,以便 json 像第一个示例 url 一样理解它...... 但是我很难在它的语法中添加 ("") java 不允许它。

【问题讨论】:

  • 你的意思是java不允许吗?你的意思是你必须逃避它?
  • 当我想添加 ("") 时出现语法错误
  • @nuestre 你能发布它的样子吗?还是上面那个?
  • 在浏览器中显示:{id:some,Categorie: cat1,Type: type1,Message: alert,Reponse: hi}
  • @nuestre 所以控制台的输出是这样的,有语法错误吗?而且我不知道你在哪里使用(“”),至于java文件将它存储在一个变量中,而不是//

标签: json xmlhttprequest getjson


【解决方案1】:

JSON 的语法非常清楚。请参阅http://www.json.org/ 您需要在字符串值 键名周围加上双引号。 {"like": "this", "see?": true}(如果您需要在字符串值中使用双引号,请使用反斜杠转义:{"this": "has \"literal\" quotes"}

至于在 Java 中编写包含双引号的字符串文字,您可以使用反斜杠 "like \"this\", see?" 转义它们

【讨论】:

  • 非常感谢@Rob Starling
  • np!我确实发现该流程图(在 json.org 上)是一个很好的视觉参考。
  • 另一件很容易抓住你的事情是尾随逗号。与 python 不同(例如),{"this": "is NOT valid",}
【解决方案2】:

因此,您似乎希望逃避引用。所以这样做

reponse_tex="{\"id\":\"" + p_id + "\",\"Categorie\": \""+ 
p_demandAide.getP_categorie()+ "\",\"Type\": \"" +p_demandAide.getP_type() 
+"\",\"Message\": \"" +p_demandAide.getP_message()+"\",\"Reponse\": \"" 
+p_demandAide.getP_response() + "\" }";

更新

也转义了键周围的引号。 \“ID\” ... 如果我不转义引号 ID,请注意区别,类别不再是字符串的一部分

reponse_tex="{"id":\"" + p_id + "\","Categorie": \""+ 
p_demandAide.getP_categorie()+ "\","Type\": "" +p_demandAide.getP_type() 
+"\","Message": \"" +p_demandAide.getP_message()+"\","Reponse": \"" 
+p_demandAide.getP_response() + "\" }";

【讨论】:

  • @nuestre LOL 也避开了 id 等之间的引号
猜你喜欢
  • 1970-01-01
  • 2016-10-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-18
  • 1970-01-01
  • 2014-06-22
  • 2017-04-04
相关资源
最近更新 更多