0

I want to write a JSON parser that parses input JSON of depth n for college assignment. As far as I have understood, I will have to convert this JSON into <String, Object> map and then create classes out of it.

Is this correct? also How would I come to know exact datatypes of the values in JSON?

for ex my sample JSON sis.

{ “name”: “user”, “address”: { "city":"abc", "zip":12345 } }

Then I am supposed to create a class named say User that has to fields 1. name: String 2. Adderss : Object and Address class having city : string and zip :int with getters and setters.

Is this correct? How to dynamically create a class? How should I start ?

user1079065
  • 2,085
  • 9
  • 30
  • 53
  • Dynamically creating classes at runtime is possible, but complicated. You'd probably be better off just using `Map` and `List`. – T.J. Crowder Dec 13 '15 at 16:42

1 Answers1

0

see this for that (not recommended): Creating classes dynamically with Java.

Advice of @T.J.Crowder is a good one: some collection.

public class json_java{
Map<String,Object> values=new HashMap<String,Object>;

To get the type:

JSONObject one_object=...
Object one_value = one_object.get("city");
one_value.getClass().getName(); // => String
Community
  • 1
  • 1