1

I have a very complicated String which looks like below,

"data":"[
         {
           "id": "123456",
           "from": 
            {
               "name": "ABC",
               "id": "123"
             },

            "message": "isafh",
            "story": "Best Story",
            "properties": 
            [
             {
               "name": "By",
               "text": "PROUD TO BE AN INDIAN",
               "href": "www.xyz.com"
             }
           ],
           "privacy": 
           {
                      "value": ""
           },
           "type": "photo",
           "created_time": "2013-10-24T07:17:28+0000",
           "updated_time": "2013-10-24T07:17:28+0000"
          },
          {
           "id": "122423456",
            "from": 
             {
                "name": "ABCaasd",
                "id": "124233"
              },

             "message": "isafh",
             "story": "Best nice Story",
             "properties": 
             [
              {
                "name": "By",
                "text": "PROUD TO BE AN INDIAN",
                "href": "www.abc.com"
              }
            ],
            "privacy": 
            {
                       "value": ""
            },
           "type": "photo",
           "created_time": "2013-10-24T07:17:28+0000",
         },
         {
           Similar data as above {... }
         },
       ]"
"next token":"1233"

Here all the JSON data is in the these brackets "[ ]" which are separated by the "{ ... }," braces.Here i want a the message,story and properties from all the curly braces. tried two things one is two put again everything in a JSON object and also tried a useless attempt to match the regex "message:" but even that didn't work.

What is the way to find the message,story and properties from all the braces.

ashubhargave
  • 230
  • 2
  • 14
  • That's not at all complicated for JSON, and any of a dozen or so different Java/JSON toolkits (see json.org) can easily handle it. – Hot Licks Oct 24 '13 at 08:42
  • 1
    Further to other answers, I'd recommend using Jackson (for which you'll probably need the [core](https://github.com/FasterXML/jackson-core) and [databind](https://github.com/FasterXML/jackson-databind) packages). – Chris Mantle Oct 24 '13 at 08:43

2 Answers2

4

Regular expressions are not suitable for parsing complex data structures such as JSON or HTML documents. Lucky for us, there are fast, reliable and free libraries out there that do this job very well.

See json.org's solution for Java, or a list of other libraries that can parse JSON listed in json.org (scroll to the bottom of the page).

Parsing a JSON document with json.org's library is as easy as the following:

String json = "{ \"message\" : \"Hi, this is the value for the key \\\"message\\\"\" }";
JSONObject myObject = new JSONObject(json); // This line actually parses the JSON text
System.out.println(myObject.getString("message"));
Ben Barkay
  • 5,473
  • 2
  • 20
  • 29
  • while using this JSONObject myObject = new JSONObject(json); it says that the constructor is not defined for String as an argument.I think i am not using the correct package.Could you please tell me which exact package should i use?Currently i am using [link](http://code.google.com/p/org-json-java/downloads/list) and [link](http://code.google.com/p/json-simple/downloads/detail?name=json-simple-1.1.1.jar&can=2&q=) jars.Thanks. – ashubhargave Oct 24 '13 at 09:52
  • @ashubhargave You can download the jar from [here](http://www.java2s.com/Code/Jar/j/Downloadjavajsonjar.htm) – Jhanvi Oct 24 '13 at 10:04
1

Use a json library like google-gson https://code.google.com/p/google-gson/

This is a working example tested with gson-1.7.1.jar but it should work with the latest version as well.

public static void main(String[] args) {
    String jsonData = "{\"data\":[{\"id\": \"123456\",\"from\": {\"name\": \"ABC\",\"id\": \"123\"},\"message\": \"isafh\",\"story\": \"Best Story\",\"properties\": [{\"name\": \"By\",\"text\": \"PROUD TO BE AN INDIAN\",\"href\": \"www.xyz.com\"}],\"privacy\": {\"value\": \"\"},\"type\": \"photo\",\"created_time\": \"2013-10-24T07:17:28+0000\",\"updated_time\": \"2013-10-24T07:17:28+0000\"},{\"id\": \"122423456\",\"from\": {\"name\": \"ABCaasd\",\"id\": \"124233\"},\"message\": \"isafh\",\"story\": \"Best nice Story\",\"properties\": [{\"name\": \"By\",\"text\": \"PROUD TO BE AN INDIAN\",\"href\": \"www.abc.com\"}],\"privacy\": {\"value\": \"\"},\"type\": \"photo\",\"created_time\": \"2013-10-24T07:17:28+0000\"}],\"next token\":\"1233\"}";
    List<String> messages = parse(jsonData);
    for (String message : messages) {
        System.out.println(message);
    }
}

public static List<String> parse(String jsonData) {
    List<String> messages = new ArrayList<String>();
    JsonElement jsonElement = new JsonParser().parse(jsonData);
    JsonObject jsonTopObject = jsonElement.getAsJsonObject();
    JsonArray jsonArray = jsonTopObject.getAsJsonArray("data").getAsJsonArray();
    Iterator<JsonElement> iterator = jsonArray.iterator();
    while (iterator.hasNext()) {
        JsonObject jsonObject = iterator.next().getAsJsonObject();
        messages.add(jsonObject.get("message").getAsString());
    }
    return messages;
}

Please note that the json data that you provided had minor changes in order to be valid. For example wrapped in "{","}" to form a json object and also at the end of the data you have "created_time": "2013-10-24T07:17:28+0000",}, so the last coma has been removed.

melc
  • 11,523
  • 3
  • 36
  • 41