Java:Gson解析没有方括号

时间:2017-02-21 18:43:49

标签: java json gson

我有一个JSON文件,我试图解析它没有周围的方括号:[...]

Here是Java代码。 (这在Maven中具有Gson依赖性)我继续在第29行获得错误,"这不是JSON数组。"但是,当我在JSON文件的开头和结尾添加方括号时,它解析得很好。如何在没有括号的情况下解析它。

这是JSON:

{
  "title": "Title",
  "content": "whatever",
  "author": "Yuki Noguchi",
  "date_published": "2017-02-15T20:25:00.000Z",
  "lead_image_url": "https://media.npr.org/assets/img/2017/02/15/ap_17039860769171_wide-d1a5d3c17f00d78fd1df9d19a96e1d7b3bd38e60.jpg?s=1400",
  "dek": null,
  "next_page_url": null,
  "url": "http://www.npr.org/2017/02/15/515425370/trump-labor-pick-andrew-puzders-nomination-appears-in-jeopardy",
  "domain": "www.npr.org",
  "excerpt": "The fast-food CEO faced fierce opposition from labor groups, plus personal controversies. Ultimately, he didn't have support from enough Republican senators.",
  "word_count": 751,
  "direction": "ltr",
  "total_pages": 1,
  "rendered_pages": 1
}

谢谢!

1 个答案:

答案 0 :(得分:0)

尝试这个,添加“[”open和“]”关闭括号并存储在临时变量中并传递该临时变量进行解析。

例如,

var jsonVar = { name:"hello", title: "WOrld" };
var tmpJsonVar = "[" + jsonVar + "]";
var jsonObject =  JSON.parse(tmpJsonVar);