-1
  • Java Object

    class B {
       private String attr;
    /***** getters and setters *****/
    
    } 
    
    class A {
          private String attr1;
          private String attr2;
          private Map<String,B> attr3;
    
        /***** getters and setters *****/
    
    }
    
  • Json Object

    json = {attr1 :"val1", attr2 : "val2", attr3 : {attr : "val"}}

How to convert json to java Object (class java contain Map as type of attribute) ?

3

4 Answers 4

2

You can use Jackson to do that:

//Create mapper instance
ObjectMapper mapper = new ObjectMapper();

//Usea a JSON string (exists other methos, i.e. you can get the JSON from a file)
String jsonString= "{'name' : 'test'}";

//JSON from String to Object
MyClass result= mapper.readValue(jsonString, MyClass .class);
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your answer, It not works when I tried to use Java Class contain a Map like the example above (class A)
1

You can use Gson library as following:

// representation string for your Json object
String json = "{\"attr1\": \"val1\",\"attr2\": \"val2\",\"attr3\": {\"attr\": \"val\"}}"; 

Gson gson = new Gson();
A a = gson.fromJson(json, A.class);

1 Comment

Did you check your answer?
0

Create a model/POJO which resembles your json structure and then by putting json string in json file you can get java object by using below simple code by using JACKSON dependacy

ObjectMapper mapper = new ObjectMapper();
File inRulesFile = (new ClassPathResource(rulesFileName + ".json")).getFile();
List<Rule> rules = mapper.readValue(inRulesFile, new TypeReference<List<Rule>>() {
        });

Comments

-1
@RunWith(SpringRunner.class)
@SpringBootTest
class PostSaveServiceTest {

    private static final String PATH_TO_JSON = "classpath:json/post-save";

    private static final String EXTENSION_JSON = ".json";

    @Test
    void setData() {

        ObjectMapper objectMapper = new ObjectMapper();

        Post post = runParseJsonFile(objectMapper);

        System.out.println(post);

    }



    private Post runParseJsonFile(ObjectMapper objectMapper) {

        File pathToFileJson = getPathToFileJson(PATH_TO_JSON + EXTENSION_JSON);

        Post post = null;

        try {
            post = objectMapper.readValue(pathToFileJson, Post.class);
        } catch (IOException e) {

            System.out.println("File didn't found : " + e);
       }

        return post;
    }


        private File getPathToFileJson(String path) {

        File pathToJson = null;

        try {

            pathToJson = ResourceUtils.getFile(path);

        } catch (IOException e) {
            e.printStackTrace();
        }


        return pathToJson;
    }
}

Comments

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.