0

I have a problem trying to parse a json file to JSON object
My JSON File is under the form :

  {  "objectJSON": [
{
 "attribute":"value",
  "attribute":"value",
  "attribute":"value",
  "attributes" :{
    "att1": "val1",
    "att2":"val2",
    "att3":"val3",
// more atts and vals may figure here ! 
  }
},
{
"attribute":"value",
  "attribute":"value",
  "attribute":"value",
  "attributes" :{
    "att1": "val1",
    "att2":"val2",
    "att3":"val3",
// more atts and vals may figure here ! 
  }
}
]
}

my problem is that the object "attributes" has an unknown number of arguments so I can not create a class for it , I thought about using a

map<String,String> attributes;

but I don't know how to parse it from the file especially that I want to keep my class "ObjectJSON" witch represents the root of my file.
I'm using gson of google
thanks in advance .

lamia Kou
  • 31
  • 3
  • Can't you use HashMap? – Poppy Apr 18 '17 at 11:04
  • Check out: `Google Gson`, `org.json` library, Jackson library, JSR-374 Json API. Each one of those you can use freely (maybe apart from org.json one, since it's quite old and crusty, albeit also pretty lightweight in comparison). – M. Prokhorov Apr 18 '17 at 11:10
  • It would be clearer if you didn't have duplicate "attribute":"value" entries. It looks like this structure corresponds to a class with a single member variable called "objectJSON" of type List>. Or, if the "attribute" entries actually have specific known names/keys then you could create a second class to represent the entries of the list - so you could have a class with a single member variable called "objectJSON" of type List, where OtherClass defines the "attribute" members and has a member called "attributes" of type Map – CAW Apr 18 '17 at 11:10

1 Answers1

0

Using org.json library:

JSONObject jsonObj = new JSONObject("{\"phonetype\":\"N95\",\"cat\":\"WP\"}");
Vikram Pawar
  • 128
  • 7