1

We have to parse a json structure similar to below.

project {
   header {
   }
   pool {
   }
   cmp {
      name = "";
      id = "";
      desc = "";
      cmp [
        {
          name = "";
          id = "";
          desc = "";
        }
        {
          name = "";
          id = "";
          desc = "";
        }
        {
          name = "";
          id = "";
          desc = "";
          cmp [
          {
            name = "";
            id = "";
            desc = "";        
          }
        }       
    }
}

The issue is, cmp element is present in the json infinitly (and it is recursive too). The cmp element contains lots of properties other than name, id and desc. But we need only name, id and desc to extract from the jSON.

I can able to parse the JSON string using com.json.parsers.JSONParser. But populating the parsed JSON to a model class/bean class is not working. It may be a simple logic. But I cannot. Please help...

The json file is generated as an output of one modeling software.

I want to parse this, using java. Can somebody help me to parse this?

Hope I have explained the issue correctly. Your help will be helpful for us.

4
  • Have you gotten as far as parsing the first level correctly? Commented Dec 17, 2013 at 13:23
  • yes. I am using com.json.parsers.JSONParser for parsing the JSON String. My deadlock is, the recursion of cmd and extracting only few properties from cmd. Commented Dec 17, 2013 at 13:26
  • You do know your example is not json right? Assuming you actually have json, have you looked at JSON libraries such as Jackson and GSON? What else have you tried? Commented Dec 17, 2013 at 13:27
  • It is JSON string only. I can able to parse the JSON string using com.json.parsers.JSONParser. But populating the parsed JSON to a model class/bean class is not working. It may be a simple logic. But I cannot. Please help... Thanks for your quick responses.. Commented Dec 17, 2013 at 13:31

2 Answers 2

2

You can do this with Jackson, just create your object with all the fields that may or may not be present in the message. All the fields not present in the message will end up as null (or default value for primitives) in the resulting object.

Just have the object contain a copy of itself and that will handle the recursion

@XmlRootElement
public class Foo {
   Foo recursiveFoo; // will be null or another instance of Foo
   int intData; // Will be 0 or an integer value
   String strData; // Will be null or a String value

   // Getters and setters here
}
Sign up to request clarification or add additional context in comments.

Comments

1

Take a look at Google Gson library. With it you can do things like:

class BagOfPrimitives {
  private int value1 = 1;
  private String value2 = "abc";
  private transient int value3 = 3;
  BagOfPrimitives() {
    // no-args constructor
  }
}

//(Serialization)
BagOfPrimitives obj = new BagOfPrimitives();
Gson gson = new Gson();
String json = gson.toJson(obj); 
//==> json is {"value1":1,"value2":"abc"}


//(Deserialization)
BagOfPrimitives obj2 = gson.fromJson(json, BagOfPrimitives.class);  
//==> obj2 is just like obj

2 Comments

But for using Gson, we need to create the model class BagOfPrimitives with all the properties in the JSON Structure right? In my JSON, some times, some element may not be present in the cmd.
That is not a problem. Suppose you have: class BagOfPrimitives { private int value1; private String value2; private int value3; BagOfPrimitives() { // no-args constructor } } and your json is: {"value1":1}. You will still be able to deserialize your object into your class, just the missing values will be with set to default (int:0, String: null)

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.