来自文件的JSON内容通过Java

时间:2016-12-04 18:46:42

标签: java json parsing json-simple

我的程序从文件中解析JSON,其中保存了此json地址的非结构化内容。

来自文件json.txt的非结构化json格式: {"total":3307,"watershed":100,"maxPage":5,"search":"http://10.103.80.149:3312/bin/sp?app=pinglun&outfmt=json&seek_timeout=400&gmt_create=1465228800~&validfeedback=1&item_id=538236063692&rate=1&layer_quota=500000&status=0,-1&order=algo_sort:des&s=0&n=20&is_wireless=0&user_id=0&utd_id=ba3290d86b9fdcd927e8ae83fde1ee64&is_click_sku=0","currentPageNum":1,"comments":[{"auction":{"title":"","thumbnail":""....

为了更好地查看结构化形式:

{
  "total": 3307,
  "watershed": 100,
  "maxPage": 5,
  "search": "http://10.103.80.162:3312/bin/sp?app=pinglun&outfmt=json&seek_timeout=400&gmt_create=1465228800~&validfeedback=1&item_id=538236063692&rate=1&layer_quota=500000&status=0,-1&order=algo_sort:des&s=0&n=20&is_wireless=0&user_id=0&is_click_sku=0",
  "currentPageNum": 1,
  "comments": [
    {
      "auction": {
        "title": "",
        "thumbnail": "",
        "aucNumId": "538236063692",
        "link": "//item.taobao.com/item.htm?id=538236063692",
        "auctionPic": "//img.alicdn.com/bao/uploaded/null_40x40.jpg",
        "sku": "机身颜色:黑色(顶级配置)[移动+联通] &nbsp套餐类型:官方标配 &nbsp存储容量:64MB &nbsp版本类型:中国大陆"
      },
      "promotionType": "活动促销 ",
      "enableSNS": false,
      "tag": "",
      "appendCanExplainable": false,
      "showCuIcon": true,
      "validscore": 1,
      "award": "",
      "noQna": true,
      "appendList": [
        {
          "photos": [

          ],
          "content": "老人机使用效果好,功能强大,非常值得拥有!!!",
          "vicious": "",
          "reply": null,
          "show": true,
          "dayAfterConfirm": 0,
          "appendId": 290673146633
        }
      ],
      "from": "",
      "date": "2016年11月05日 16:46",
      "dayAfterConfirm": 0,
      "bidPriceMoney": {
        "cent": 7905,
        "amount": 79.05,
        "currencyCode": "CNY",
        "centFactor": 100,
        "displayUnit": "元",
        "currency": {
          "defaultFractionDigits": 2,
          "currencyCode": "CNY",
          "symbol": "¥"
        }
      },
      "rate": "1",
      "o2oRate": null,
      "propertiesAvg": "0.0",
      "showDepositIcon": false,
      "rateId": 290589793921,
      "creditFraudRule": 0,
      "useful": 0,
      "reply": null,
      "append": {
        "photos": [

        ],
        "content": "老人机使用效果好,功能强大,非常值得拥有!!!",
        "vicious": "",

.
.
.
.}

我只需解析元素“content”中的中文注释,例如“老人机使用效果好,功能强大,非常值得拥有!!!”,但我不知道如何使用json-simple在JSONArray中使用这个元素。

以下是代码示例:

public class Final {
    public static void main(String[] args) throws IOException, JSONException {
        String jsonData = "";
        BufferedReader br = null;
        try {
            String line;
            br = new BufferedReader(new FileReader("json.txt"));
            while ((line = br.readLine()) != null) {
                jsonData += line + "\n";
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (br != null)
                    br.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
        System.out.println("\nFile Content: \n" + jsonData1); // displays all content
        JSONObject obj = new JSONObject(jsonData);
        System.out.println("search: " + obj.getString("search"));
        System.out.println("comments: " + obj.getJSONObject("comments"));
}
}

你能帮我解决这个问题吗?

0 个答案:

没有答案