用Java解码JSON字符串

我是Java中使用json-simple库的新手,我已经完成了编码和解码示例。 复制编码示例很好,但是我无法使用混合类型的JSON来解码。

我的一个问题是库中有太多的类没有正确记录,而且我没有源(为了能够阅读并理解它们的用途)。 因此,我很难理解如何使用这些类。

看完这个例子后:

String jsonText = "{\"first\": 123, \"second\": [4, 5, 6], \"third\": 789}"; JSONParser parser = new JSONParser(); ContainerFactory containerFactory = new ContainerFactory(){ public List creatArrayContainer() { return new LinkedList(); } public Map createObjectContainer() { return new LinkedHashMap(); } }; try { Map json = (Map)parser.parse(jsonText, containerFactory); Iterator iter = json.entrySet().iterator(); System.out.println("==iterate result=="); while(iter.hasNext()) { Map.Entry entry = (Map.Entry)iter.next(); System.out.println(entry.getKey() + "=>" + entry.getValue()); } System.out.println("==toJSONString()=="); System.out.println(JSONValue.toJSONString(json)); } catch(ParseException pe) { System.out.println(pe); } 

从json简单的官方解码教程 ,我试图解码这个JSON:

 { "stat":{ "sdr": "MAC address of FLYPORT", "rcv": "ff:ff:ff:ff:ff:ff", "time": "0000000000000", "type": 0, "subt": 0, "argv": [ {"type": "6","val": "NetbiosName"}, {"type": "6","val": "MACaddrFlyport"}, {"type": "6","val": "FlyportModel"}, {"type": "1","val": id} ] } } 

我正在编写以下代码来解码:

  String jsonString = "{\"stat\":{\"sdr\": \"aa:bb:cc:dd:ee:ff\",\"rcv\": \"aa:bb:cc:dd:ee:ff\",\"time\": \"UTC in millis\",\"type\": 1,\"subt\": 1,\"argv\": [{1,2},{2,3}]}}"; JSONObject jsonObject = new JSONObject(jsonString); JSONObject newJSON = jsonObject.getJSONObject("stat"); System.out.println(newJSON); 

但它不起作用。 事实上,我无法使未经修改的示例工作,并且原作者没有解释他们的代码。

如图所示解码此JSON的最简单方法是什么?

这是最好最简单的代码:

 public class test { public static void main(String str[]) { String jsonString = "{\"stat\": { \"sdr\": \"aa:bb:cc:dd:ee:ff\", \"rcv\": \"aa:bb:cc:dd:ee:ff\", \"time\": \"UTC in millis\", \"type\": 1, \"subt\": 1, \"argv\": [{\"type\": 1, \"val\":\"stackoverflow\"}]}}"; JSONObject jsonObject = new JSONObject(jsonString); JSONObject newJSON = jsonObject.getJSONObject("stat"); System.out.println(newJSON.toString()); jsonObject = new JSONObject(newJSON.toString()); System.out.println(jsonObject.getString("rcv")); System.out.println(jsonObject.getJSONArray("argv")); } } 

这里给出了json文件的库定义 。 它与此处发布的库不同,即由您发布。 您发布的是我使用过这个库的 简单json 库 。

你可以下载拉链 。 然后使用org.json作为名称在项目中创建一个package 。 并将所有下载的代码粘贴在那里,玩得开心。

我觉得这是最好和最简单的JSON解码。

那你的jsonString是错的。

 String jsonString = "{\"stat\":{\"sdr\": \"aa:bb:cc:dd:ee:ff\",\"rcv\": \"aa:bb:cc:dd:ee:ff\",\"time\": \"UTC in millis\",\"type\": 1,\"subt\": 1,\"argv\": [{\"1\":2},{\"2\":3}]}}"; 

使用这个jsonString ,如果在示例中使用相同的JSONParserContainerFactory ,您将看到它将被编码/解码。

另外,如果你想在stat之后打印你的字符串,它会:

  try{ Map json = (Map)parser.parse(jsonString, containerFactory); Iterator iter = json.entrySet().iterator(); System.out.println("==iterate result=="); Object entry = json.get("stat"); System.out.println(entry); } 

关于json库,有很多它们。 你最好检查一下 。

您可以将此JAR文件添加到您的包中,而不是按照Veer的建议下载单独的Java文件。

要在Eclipse中将jar文件添加到项目中,请执行以下操作:

  1. 右键单击您的项目,单击“构建路径”>“配置构建路径”
  2. “转到库”选项卡>“添加外部JAR”
  3. 找到JAR文件并添加

这是我们要解码的JSON字符串:

 { "stats": { "sdr": "aa:bb:cc:dd:ee:ff", "rcv": "aa:bb:cc:dd:ee:ff", "time": "UTC in millis", "type": 1, "subt": 1, "argv": [ {"1": 2}, {"2": 3} ]} } 

我将此字符串存储在变量名称“sJSON”下现在,这是如何解码它:)

 // Creating a JSONObject from a String JSONObject nodeRoot = new JSONObject(sJSON); // Creating a sub-JSONObject from another JSONObject JSONObject nodeStats = nodeRoot.getJSONObject("stats"); // Getting the value of a attribute in a JSONObject String sSDR = nodeStats.getString("sdr");