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

时间:2011-02-24 15:45:56

标签: java python json response

我已经使用了几个不同的API,我需要解析JSON。在所有情况下,响应的构造有点不同。

我现在需要通过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.JSONException:类型为org.json.JSONObject的[json string]无法转换为JSONArray

但是在Python中,我能够接受这个响应,获取“结果”,然后像解析任何其他JSON数据一样进行解析。

*更新*

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

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

你究竟是如何产生这种反应的?你是自己做的吗?

我发现你在publishers的最后一个元素的末尾有一个悬空的逗号(即在值Kaissa Chess & Games之后)。

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