java 中org.json的用法

一,工程pom文件中新增依赖,参考http://mvnrepository.com/artifact/org.json/json/20160212

    <dependency>
    <groupId>org.json</groupId>
    <artifactId>json</artifactId>
    <version>20160212</version>
    </dependency>

二、用到的部分代码,用Json 字符串生成json对象,再从中获取需要的字段

        /* 解析消息 */
        String clientMsg = tuple.getStringByField("clientInfo");
        
        /* covert string to json object */
        JSONObject clientMsgObject = new JSONObject(clientMsg); 
        
        String acSNStr = clientMsgObject.getString("acSN");
        String clientMacStr = clientMsgObject.getString("clientMAC");
        String onLineTimeStr = clientMsgObject.getString("onLineTime");

三、lib源码

https://github.com/stleary/JSON-java

四、javadoc

https://search.maven.org/#search%7Cgav%7C1%7Cg%3A%22org.json%22%20AND%20a%3A%22json%22

原文地址:https://www.cnblogs.com/zhengchunhao/p/5644431.html