2013-05-15 113 views
13

我是使用Java中的json-simple库的新手,我已经通过了encodingdecoding样本。复制编码的例子很好,但我一直无法得到解码与混合类型JSON的工作。在Java中解码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-simple official decoding tutorial

,我试图解码此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的最简单方法是什么?

+0

我们可以看到您的代码用于解码stat吗? –

+0

您是否在[JSON官方解码教程](https://code.google.com/p/json-simple/wiki/DecodingExamples)中检查了示例5(可停止类SAX的内容处理程序) 正如我所见,该代码旨在用于单个字典(Map)/ Array。但是你正在尝试使用地图中的json(多个地图)。如果我错了,纠正我。 – ram2013

+0

@ ram2013:你..我需要解码的是地图中的地图...我想这是其中之一。就好像我获得“stat”的值一样,我可以将它用于进一步的JSON解码。 –

回答

17

这是最好的和最简单的代码:

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")); 
    } 
} 

library definition of the json files are given here。并且它不是发布的here,即由您发布的图书馆。你发布的是simple json library我已经使用this library

您可以download the zip。然后在org.json作为名称在您的项目中创建一个package。并粘贴所有下载的代码,并获得乐趣。

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

+0

我们如何在'JSON'中将'\'添加到''''我不想做'String.replace()' – Identity1

+0

我一直得到这个输出 “ JSONObject文本必须以'{'开始,位于1 [character 2 line 1]“ 我的JSON内容在第二个之前没有任何内容”{“ – Chandough

+0

请更新链接。其死亡 – muaaz

4

那么你的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库,也有很多人。最好检查一下this

+0

链接已死... –

2

这是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"); 
+1

Letter's'is missing a t“stat”,应该是JSONObject nodeStats = nodeRoot.getJSONObject(“stats”);对 ? –

3

不像Veer建议的那样下载单独的java文件,您可以将这个JAR file添加到您的包中。

JAR文件添加到Eclipse中的项目,请执行以下操作:

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