简体   繁体   中英

How to convert JSON Array of Values to Java Object

I am trying to convert my below Json Array to a specific Java Object.

[
    {
        "Dummy": {
            "attr": "PRIMITIVE",
            "value": {
                "Initial": "M",
                "Name": "Potato"
            }
        }
    },
    {
        "Dummy": {
            "attr": "PRIMITIVE",
            "value": {
                "Initial": "A",
                "Name": "Potatoo1"
            }
        }
    }
]

Edit: Updated the correct JSON structure

Below is the object Mapper I am currently using:

     ObjectMapper mapper = new ObjectMapper();
        Dummy[] myObjects = mapper.readValue(resultStr, Dummy[].class);
    
public class Dummy{

    public String attr;
    public value val;
}

public class value {

    public String Initial;
    public String Name;
}

However I am getting error while trying to deserialize com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize value of type com.example.demo.Dummy from Array value

Your input is no valid JSON data, use a JSON formatter/validator to check (for example https://jsonformatter.curiousconcept.com )

I assume, the "correct" JSON for your data would be something like the following:

[
    {
        "Dummy": {
            "attr": "PRIMITIVE",
            "value": {
                "Initial": "M",
                "Name": "Potato"
            }
        }
    },
    {
        "Dummy": {
            "attr": "PRIMITIVE",
            "value": {
                "Initial": "A",
                "Name": "Potatoo1"
            }
        }
    }
]

This JSON can be mapped like this:

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;


public class OMTest {

    public void deserialize() throws JsonMappingException, JsonProcessingException {
        String json = "[{\"Dummy\": {\"attr\": \"PRIMITIVE\",\"value\": {\"Initial\": \"M\",\"Name\": \"Potato\"}}},{\"Dummy\": {\"attr\": \"PRIMITIVE\",\"value\": {\"Initial\": \"A\",    \"Name\": \"Potatoo1\"}}}]";

        ObjectMapper mapper = new ObjectMapper();
        YourClass[] myObjects = mapper.readValue(json, YourClass[].class);

        // do something...
        System.out.println(myObjects.length);
    }

    private static class YourClass {
        @JsonProperty("Dummy")
        public Dummy dummy;
    }

    private static class Dummy {
        public String attr;
        public Value value;
    }

    private static class Value {
        @JsonProperty("Initial")
        public String initial;
        @JsonProperty("Name")
        public String name;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM