简体   繁体   English

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

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

I've worked with several different APIs where I needed to parse JSON. 我使用了几种需要解析JSON的API。 And in all cases the Response is constructed a bit differently. 在所有情况下,响应的构造都有些不同。

I now need to expose some data via a JSON API and want to know the proper way to deliver that Response. 我现在需要通过JSON API公开一些数据,并想知道传递响应的正确方法。

Here is an example of what I have now, however some users (one using Java) are having difficulty parsing. 这是我现在拥有的示例,但是某些用户(一个使用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"
      }
    }

The Java user in particular is complaining that they get the error: 特别是Java用户抱怨他们得到了错误:

org.json.JSONException:[json string] of type org.json.JSONObject cannot be converted to JSONArray org.json.JSONObject类型的org.json.JSONException:[json字符串]无法转换为JSONArray

But in Python I am able to take in this Response, fetch "result" and then parse as I would any other JSON data. 但是在Python中,我可以接受此Response,获取“结果”,然后像解析任何其他JSON数据一样进行解析。

* UPDATE * *更新*

I passed both my JSON and Twitter's timeline JSON to JSONLint. 我将JSON和Twitter的时间轴JSON都传递给JSONLint。 Both are valid. 两者均有效。 The Java user can parse Twitter's JSON but not mine. Java用户可以解析Twitter的JSON,但不能解析我的。 What I noticed with Twitter's JSON is that it's encapsulated with brackets [], signifying an array. 我在Twitter的JSON中注意到的是,它用方括号[]封装,表示一个数组。 And the error this user is getting with my JSON is that it cannot be converted to a JSON array. 该用户使用我的JSON遇到的错误是无法将其转换为JSON数组。 I didn't think I need to encapsulate in brackets. 我认为我不需要封装在方括号中。

It looks valid according to http://json.parser.online.fr/ (random json parser). 根据http://json.parser.online.fr/ (随机json解析器),它看起来有效。 Its in the other code i'd say ;) 在其他代码中我会说;)

How exactly are you generating this response? 您究竟如何生成此响应? Are you doing it yourself? 你自己做吗?

I see you have a dangling comma at the end of the last element in publishers (ie after the value Kaissa Chess & Games ). 我看到您在publishers的最后一个元素的末尾有一个悬挂的逗号(即,在值Kaissa Chess & Games )。

I'd recommend using JSONLint to ensure your JSON is valid. 我建议使用JSONLint来确保您的JSON有效。

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

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