0

I have following type of json data and I want extract title from data using java regular expression

{
    "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"
}

please tell me for extract title from data what will be regular expresion

Cœur
  • 37,241
  • 25
  • 195
  • 267
Aakash Kag
  • 362
  • 6
  • 16
  • 9
    Do not use regex. Use a JSON parser. – Jens Apr 14 '15 at 07:09
  • I suggest you take a look at the org.json library (import org.json.*) rather than using regex. – Danilo Apr 14 '15 at 07:10
  • Thanx for quick reply i alredy tried with json parser but i got left '[ ' error – Aakash Kag Apr 14 '15 at 07:10
  • you can also use GSON for the same. – Pratik Apr 14 '15 at 07:11
  • is there is any gui based tool available which can parse my json data easily,,,actually i want extract title from json file,,, – Aakash Kag Apr 14 '15 at 07:15
  • @AakashKag - This is one of 5 easy to find posts on this topic. Here's one: http://stackoverflow.com/questions/8750127/regex-for-parsing-single-key-values-out-of-json-in-javascript I'm not gonna DV you, but for future reference: Before posting on stackoverflow, it you aren't gonna google search your question, at least search for it on stack overflow. It even prompts you to before asking anything each time. – that-ru551an-guy Apr 14 '15 at 07:20
  • Look at http://json.org. There are lots of tools. (But how / why you would a "GUI based tool" for parsing JSON escapes me ...) – Stephen C Apr 14 '15 at 07:47

1 Answers1

3

Use a JSON parser:

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

Output:

nice television for the price