用Java解码JSON字符串

时间:2013-05-15 20:34:48

标签: java json json-simple

我不熟悉使用Java中的json-simple库,而且我已经通过了encodingdecoding样本。复制编码示例很好,但是我无法使用混合类型的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的最简单方法是什么?

4 个答案:

答案 0 :(得分:21)

这是最好最简单的代码:

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解码。

答案 1 :(得分: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

答案 2 :(得分:3)

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

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

  1. 右键单击您的项目,单击Build Path>配置构建路径
  2. 转到图书馆标签>添加外部JAR
  3. 找到JAR文件并添加

答案 3 :(得分: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");