-1
${
   "1":{
      "vertical_id":"1",
      "vertical_name":"Men",
      "url_key":"men",
      "meta_title":"Online Shopping For Men - Buy Mens Clothes, Footwear & Accessories At Fetise ",
      "meta_description":null,
      "meta_keywords":"mens apparel, mens clothing, mens accessories online, online mens footwear store, gadgets online for men\r\n",
      "intro_banner_path":"",
      "intro_heading":"",
      "intro_subheading":"",
      "show_products":"1",
      "featured_event_count":"8",
      "is_active":"1",
      "sort_order":"1",
      "twitter_handle":"fetise",
      "facebook_page":"fetise",
      "verticalUrl":"http:\/\/www.fetise.com\/men",
      "verticalUrlComponent":"men\/"
   },
   "2":{
      "vertical_id":"2",
      "vertical_name":"Women",
      "url_key":"women",
      "meta_title":"Online Shopping for Women - Buy Womens Clothes, Footwear & Accessories At Fetise",
      "meta_description":null,
      "meta_keywords":"online womens clothing store, womens apparels online, ladies footwear and accessories store, ladies personal care products online, womens fashion clothes\r\n",
      "intro_banner_path":"",
      "intro_heading":"",
      "intro_subheading":"",
      "show_products":"1",
      "featured_event_count":"0",
      "is_active":"1",
      "sort_order":"2",
      "twitter_handle":"FetiseWomen",
      "facebook_page":"FetiseWomen",
      "verticalUrl":"http:\/\/www.fetise.com\/women",
      "verticalUrlComponent":"women\/"
   }}

How can I parse this JSON in java?

pb2q
  • 58,613
  • 19
  • 146
  • 147
Ajit
  • 215
  • 2
  • 8

1 Answers1

0

I've used both GSON and Jackson, and eventually settled on Jackson. It better suited a particular java project that required JSON parsing, and now just seems more flexible and/or familiar.

Using the Jackson Tree Model, after discarding the leading $ (invalid JSON):

// input is your JSON object
ObjectMapper mapper = new ObjectMapper();

// could also use Jackson ObjectNodes:
JsonNode rootNode = mapper.readValue(input.getBytes(), JsonNode.class); 
JsonNode sub1 = rootNode.get("1");
JsonNode sub2 = rootNode.get("2");

if (sub1 != null)
{
    System.out.println("1.vertical_name: " + sub1.get("vertical_name"));
    System.out.println("1.meta_title: " + sub1.get("meta_title"));
}

if (sub2 != null)
{
    System.out.println("2.vertical_name: " + sub2.get("vertical_name"));
    System.out.println("2.meta_title: " + sub2.get("meta_title"));
}

Output:

1.vertical_name: "Men"
1.meta_title: "Online Shopping For Men - Buy Mens Clothes, Footwear & Accessories At Fetise "
2.vertical_name: "Women"
2.meta_title: "Online Shopping for Women - Buy Womens Clothes, Footwear & Accessories At Fetise"

Your input JSON is idiosyncratic in that it appears to be using a JSON object as a JSON array, with the outer object having fields 1, 2, .... So the above approach parses the JSON as generic JsonNode objects, though you might just as well use Jackson to parse this into java Maps - see Data Binding with Generics.

Navigating the resulting data structure assumes knowledge of the input, i.e. using JsonNode.get("1");, but you'll need to make the same assumptions regardless of how you represent the JSON.

You can also use JsonNode.iterator to iterate over an unknown number of such "indices". This is useful if you don't know the number of 1, 2, etc.

Furthermore, you can make a java class that matches each sub-object, and use Jackson to create instances of that class given each sub-object. See Full Data Binding (POJO) Example.

pb2q
  • 58,613
  • 19
  • 146
  • 147
  • Thanks Brother.Actually I am New to this Json.Now i want to sore these Json value in 2 Bean classes like MEN & WOMEN>how can i do it.Please give some Hint. – Ajit Jul 27 '12 at 09:07
  • IF if i get the json data from URl so it dificult for me to know JsonNode id.may be its more than 2.At that time how can get the value. – Ajit Jul 27 '12 at 09:24
  • See the bit in the answer about `iterator`: this will give you a list of the top-level attributes from the JSON object, so that you don't need to do individual `get` calls over "1", "2", "3" – pb2q Jul 27 '12 at 21:20