0

In my project i am dynamically generating json and put that into Map<String,Student>() Note:Key is dynamic so my response is printing like below :

"entry": [
        {
          "key": "student1",
          "value": {
            "name":selva
          }
        },
        {
          "key": "student2",
          "value": {
           "name":"kumar"
          }
        }
       ]

But my expected response is :

[
"student1" : {
"name":"kumar"
},
"student2" : {
"name":"selva"
}
]

**My keys are dynamic here...**

How to achiveve the desired result in jersey. pom.xml

<!-- Json dependency -->
        <dependency>
            <groupId>org.json</groupId>
            <artifactId>json</artifactId>
        </dependency>
<dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-moxy</artifactId>
        </dependency>

Any help will be greatly appreciated!!!!

Selva
  • 1,620
  • 3
  • 33
  • 63
  • What json library you are using? – LynxZh Jun 16 '16 at 09:45
  • What JSON provider are you using? If you are using Jackson, you should not be getting that result. I think MOXy will give you that result. If you are using MOXy, switch to Jackson. Otherwise if you have a different scenario, please post your dependencies – Paul Samsotha Jun 16 '16 at 09:45
  • Added my dependencies in my post – Selva Jun 16 '16 at 09:52
  • Get rid of moxy and use `jersey-media-json-jackson` – Paul Samsotha Jun 16 '16 at 10:11
  • @peeskillet,This is not duplicate question.In my question json key also dynamic but key is static on that question.that's why i am posted here – Selva Jun 16 '16 at 10:45
  • Use Jackson. That's your solution. If you don't want to swtich to Jackson, then I will re-open the question. Otherwise with MOXy you going to have a tough time. MOXy and maps do not work well together – Paul Samsotha Jun 16 '16 at 10:48
  • i tried with jersey-media-json-jackson but still not working @peeskillet – Selva Jun 16 '16 at 10:53
  • Probably because you didnt remove the MOXY dependency. If you have both, it will default to using MOXy – Paul Samsotha Jun 16 '16 at 10:56
  • i removed that.But still not working – Selva Jun 16 '16 at 11:46
  • So you're saying the result is exactly the same? Are you using Glassfish server? That's the only way I could see it not working. If you are using Glassfish, you can explicitly register the `JacksonFeature`. In Glassfish, there is also MOXy already in the server libraries. So you can disable it by explicitly registering Jackson. If you are not using Glassfish, I don't know what to tell you. I'll reopen your question, but I know for a fact that Jackson has no problems with maps. – Paul Samsotha Jun 16 '16 at 12:21
  • @peeskillet,yes i am using glassfish server only – Selva Jun 17 '16 at 10:20
  • So you have two options. 1) Explicitly register the `JacksonFeature`. 2) Set this Jersey property `jersey.config.server.disableMoxyJson` to `true`. Either one of these will disable MOXy – Paul Samsotha Jun 17 '16 at 10:52

1 Answers1

1
/**
     * JSONObjects can be built from a Map<String, Object>. 
     * In this test all of the map entries are valid JSON types.
     */
    @Test
    public void jsonObjectByMap() {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("trueKey", new Boolean(true));
        map.put("falseKey", new Boolean(false));
        map.put("stringKey", "hello world!");
        map.put("escapeStringKey", "h\be\tllo w\u1234orld!");
        map.put("intKey", new Long(42));
        map.put("doubleKey", new Double(-23.45e67));
        JSONObject jsonObject = new JSONObject(map);

        // validate JSON
        Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
        assertTrue("expected 6 top level items", ((Map<?,?>)(JsonPath.read(doc, "$"))).size() == 6);
        assertTrue("expected \"trueKey\":true", Boolean.TRUE.equals(jsonObject.query("/trueKey")));
        assertTrue("expected \"falseKey\":false", Boolean.FALSE.equals(jsonObject.query("/falseKey")));
        assertTrue("expected \"stringKey\":\"hello world!\"", "hello world!".equals(jsonObject.query("/stringKey")));
        assertTrue("expected \"escapeStringKey\":\"h\be\tllo w\u1234orld!\"", "h\be\tllo w\u1234orld!".equals(jsonObject.query("/escapeStringKey")));
        assertTrue("expected \"doubleKey\":-23.45e67", Double.valueOf("-23.45e67").equals(jsonObject.query("/doubleKey")));
}
PhungHV
  • 82
  • 2