-1

I have the following type of JSON I want to send to Java (I'm using Jersey and the default JSON Parser it comes with)

{ "something" : "1", "someOtherThing" : "2" , ... }

But instead of creating an Object with all these properties in Java, I would like to have a Single HashMap (or whatever) that will allow me to still have access to the Key and the Value

Is such a thing possible?

I don't really have any code that does the transformation, I use Jersey like this

    @POST
    @Path("/purchase")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    public StatusResult purchase(UserPurchaseRequest upr) {     
    }

If i put properties something and someOtherThing as Strings in my UserPurchaseRequest object, everything will come in fine, but I want to have everything in one structure (because I don't know how many values I will get, and I need their names as well)

sahmed24
  • 358
  • 2
  • 3
  • 13

2 Answers2

0

Yes, it is possible. But still, it depends on what JSON java API you are using. For example using Jackson JSON you can create HashMap json string like this

  ObjectMapper obj = new ObjectMapper();
  String json = pbj.writeValue(<HashMap object>);

or vice-versa

  HashMap obj = obj.readValue(json, HashMap.class);

Note - org.codehaus.jackson.map.ObjectMapper

IndoKnight
  • 1,846
  • 1
  • 21
  • 29
0

You just need to add a Property to your Object like this

    private HashMap<String,String> purchaseValues;

Jersey takes care of the rest, for some reason while you are debugging, most of the entries appear as null in the HashMap

sahmed24
  • 358
  • 2
  • 3
  • 13