【问题标题】:Parse json data using java regular expression使用java正则表达式解析json数据
【发布时间】:2015-06-19 16:47:57
【问题描述】:

我有以下类型的 json 数据,我想使用 java 正则表达式从数据中提取标题

{
    "Title": "nice television for the price",
    "Author": "Jackie C",
    "ReviewID": "R3LDJA7HU2Q0FS",
    "Overall": "4.0",
    "Content": "The television was a refurbished one, and for the price, quality of pictures, sound, i am enjoying it at this very moment. I'm glad that I made a good choice thus far without having any problems with the television.  Thanks",
    "Date": "April 13, 2014"
}

请告诉我从数据中提取标题的正则表达式是什么

【问题讨论】:

  • 不要使用正则表达式。使用 JSON 解析器。
  • 我建议你看看 org.json 库 (import org.json.*) 而不是使用正则表达式。
  • 感谢快速回复,我已经尝试过使用 json 解析器,但我得到了 '[ ' 错误
  • 您也可以使用 GSON。
  • 是否有任何基于 gui 的工具可以轻松解析我的 json 数据,,,实际上我想从 json 文件中提取标题,,,

标签: java regex


【解决方案1】:

使用 JSON 解析器:

import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;

public class SOPlayground {

    public static void main(String[] args) throws Exception {
        String j = "{\n"
                + "    \"Title\": \"nice television for the price\",\n"
                + "    \"Author\": \"Jackie C\",\n"
                + "    \"ReviewID\": \"R3LDJA7HU2Q0FS\",\n"
                + "    \"Overall\": \"4.0\",\n"
                + "    \"Content\": \"The television was a refurbished one, and for the price, quality of pictures, sound, i am enjoying it at this very moment. I'm glad that I made a good choice thus far without having any problems with the television.  Thanks\",\n"
                + "    \"Date\": \"April 13, 2014\"\n"
                + "}";

        JSONParser p = new JSONParser();
        JSONObject o = (JSONObject) p.parse(j);
        System.out.println(o.get("Title"));
    }
}

输出:

nice television for the price

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-10
    • 1970-01-01
    • 1970-01-01
    • 2013-07-08
    相关资源
    最近更新 更多