1

This is the sample of a JSON object. I want to extract the part of it and display those values in a jTable (SWING) in JAVA. Keys as table column names and values as row data respectively.

[
   {
      "_id":{
         "date":"xxxxxxxx",
         "time":"xxxxxxx",
         "inc":"xxxx"
      },
      "DOCUMENTS":[
         {
            "EName":"John",
            "eAge":"25",
            "eAddress":"UK"
         },

         {
            "EName":"Alex",
            "eAge":"24",
            "eAddress":"Australia"
         }
      ]
   }
]

I want to extract this part.

      [
         {
            "EName":"John",
            "eAge":"25",
            "eAddress":"UK"
         },
         {
            "EName":"Alex",
            "eAge":"24",
            "eAddress":"Australia"
         }
      ]

I used this way to get the answer. Here jsonstring is the string that contains above data.

        String[] splits = jsonString.split("DOCUMENTS\":");
        String[] splits2 = splits[1].split("\\}]", 2);
        System.out.println("spilted  :"+splits2[0]);

but it is giving me the answer as

[{"EName":"John","eAge":"25","eAddress":"UK"}, 
 {"EName":"Alex","eAge":"24","eAddress":"Australia"

it removed the closed square bracket.

How can I get the correct answer? Your help is much appreciated.

5
  • 1
    Why not use a JSON Parser and get the Documents JSONArray? Commented Nov 17, 2014 at 20:33
  • You can use JSONPointer with the query: /0/DOCUMENTS Commented Nov 17, 2014 at 20:33
  • it removed the closed square bracket - Since u split on "}]" - split2 Commented Nov 17, 2014 at 20:41
  • @MiteshPathak . I want to remove the last }] characters. How can I do it? Commented Nov 17, 2014 at 20:46
  • hmm... int len = split1[1].length(); String ans = split1[1].substring(0, len - 3); // assuming last 2 chars are ] and } Commented Nov 17, 2014 at 20:49

2 Answers 2

2

Take a look at this simple tutorials: http://wiki.fasterxml.com/JacksonInFiveMinutes http://www.tutorialspoint.com/json/json_java_example.htm

example:

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

public class KroplaTest {
    private static final String JSON = "{\"id\":{ "
            + "\"date\":\"xxxxxxxx\","
            + "\"time\":\"xxxxxxx\","
            + "\"inc\":\"xxxx\""
            + "},"
            + "\"documents\":["
            + "{"
            + " \"eName\":\"John\","
            + " \"eAge\":\"25\","
            + " \"eAddress\":\"UK\""
            + "},"
            + "{"
            + " \"eName\":\"Alex\","
            + " \"eAge\":\"24\","
            + " \"eAddress\":\"Australia\""
            + "} ]} ";

    public static void main(String[] args) {
        final ObjectMapper mapper = new ObjectMapper();
        try {
            Map<String,Object> map = new HashMap<String,Object>();
            map = mapper.readValue(JSON, new TypeReference<HashMap<String,Object>>(){});
            System.out.println(map.get("documents").toString());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

This will print to sysout:

[{eName=John, eAge=25, eAddress=UK}, {eName=Alex, eAge=24, eAddress=Australia}]
Sign up to request clarification or add additional context in comments.

Comments

0

Instead of using split(), use:

int index= splits[1].indexOf("\\}]");
String result = splits[1].substring(0,index);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.