用Java解码JSON字符串 [英] Decoding JSON String in Java

查看:110
本文介绍了用Java解码JSON字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新手使用Java中的json-simple库而且我已经通过编码解码示例。复制编码示例很好,但是我无法使用混合类型的JSON来解码它们。

I am new to using the json-simple library in Java and I've been through both the encoding and decoding samples. Duplicating the encoding examples was fine, but I have not been able to get the decoding ones to work with mixed type JSON.

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

One of my problems is that there are too many classes in the library which are not properly documented, and for which I do not have the source (in order to be able to read through and understand their purpose). Consequently, I am struggling to understand how to use a lot of these classes.

阅读此示例后:

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

/ p / json-simple / wiki / DecodingExamplesrel =noreferrer> json-simple官方解码教程,我试图解码这个JSON:

from the json-simple official decoding tutorial, I tried to decode this 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}
    ]
}
}

我正在编写以下代码进行解码:

I am writing following code to decode:

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

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

But it doesn't work. Infact I was not able to get the unmodified example working either, and the original authors have not explained their code.

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

What is the easiest way to decode this JSON as shown?

推荐答案

这是最好最简单的代码:

This is the best and easiest code:

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库我用过这个库

The library definition of the json files are given here. And it is not same libraries as posted here, i.e. posted by you. What you had posted was simple json library I have used this library.

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

You can download the zip. And then create a package in your project with org.json as name. and paste all the downloaded codes there, and have fun.

我认为这是最好,最简单的JSON解码。

I feel this to be the best and the most easiest JSON Decoding.

这篇关于用Java解码JSON字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆