繁体   English   中英

JSON响应的正确格式是什么?

[英]What is the proper format for a JSON Response?

我使用了几种需要解析JSON的API。 在所有情况下,响应的构造都有些不同。

我现在需要通过JSON API公开一些数据,并想知道传递响应的正确方法。

这是我现在拥有的示例,但是某些用户(一个使用Java)在解析时遇到困难。

    {"status": "200 OK", 
    "code": "\/api\/status\/ok", 
    "result": {
      "publishers": ["Asmodee", "HOBBITY.eu", "Kaissa Chess & Games"], 
      "playing_time": 30, "description": "2010 Spiel des Jahres WinnerOne player is the storyteller for the turn. He looks at the 6 images in his hand. From one of these, he makes up a sentence and says it out loud (without showing the card to the other players).The other players select amongst their 6 images the one that best matches the sentence made up by the storyteller.Then, each of them gives their selected card to the storyteller, without showing it to the others. The storyteller shuffles his card with all the received cards. ",   
      "expansions": ["Dixit 2", "Dixit 2: \"Gift\" Promo Card", "Dixit 2: The American Promo Card", "Dixit Odyssey"], 
      "age": 8, 
      "min_players": 3, 
      "mid": "\/m\/0cn_gq3", 
      "max_players": null,  
      "designers": ["Jean-Louis Roubira"], 
      "year_published": 2008, 
      "name": "Dixit"
      }
    }

特别是Java用户抱怨他们得到了错误:

org.json.JSONObject类型的org.json.JSONException:[json字符串]无法转换为JSONArray

但是在Python中,我可以接受此Response,获取“结果”,然后像解析任何其他JSON数据一样进行解析。

*更新*

我将JSON和Twitter的时间轴JSON都传递给JSONLint。 两者均有效。 Java用户可以解析Twitter的JSON,但不能解析我的。 我在Twitter的JSON中注意到的是,它用方括号[]封装,表示一个数组。 该用户使用我的JSON遇到的错误是无法将其转换为JSON数组。 我认为我不需要封装在方括号中。

根据http://json.parser.online.fr/ (随机json解析器),它看起来有效。 在其他代码中我会说;)

您究竟如何生成此响应? 你自己做吗?

我看到您在publishers的最后一个元素的末尾有一个悬挂的逗号(即,在值Kaissa Chess & Games )。

我建议使用JSONLint来确保您的JSON有效。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM