1

I have the Json data like {"no":["1","2","3"],"date":["23/05/1992","02/01/1991","01/05/1992"]} I want to split in to correct format in java.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
V. Monisha
  • 89
  • 6

3 Answers3

2

Two main ways :

1) Define a class to map it :

public class Foo{

   private List<String> no;
   private List<LocalDate> date;
   // setters or factory method
}

And use a Json API such as Jackson :

ObjectMapper mapper = new ObjectMapper();
Foo foo = mapper.readValue(myStringRepresentingJson, Foo.class)

You could need to use and to set a JsonDateSerializer instance to specify the date format.

2) Define a custom JSON deserializer.

It allows to control more finely and programmatically the way to map json attributes to a Java object.
With Jackson, extending the class StdDeserializer is a possibility.

davidxxx
  • 125,838
  • 23
  • 214
  • 215
  • If i am using this i got org.codehaus.jackson.map.JsonMappingException: Can not deserialize instance of java.util.ArrayList out of VALUE_STRING token exception – V. Monisha Jul 12 '19 at 07:05
  • It means that for the `no` attribute you don't pass a JSON array but a string as input. Try to hardcode it to test it : `String value = "{\"no\":[\"1\",\"2\",\"3\"],\"date\":[\"23/05/1992\",\"02/01/1991\",\"01/05/1992\"]}";Foo foo = mapper.readValue(value, Foo.class);` – davidxxx Jul 12 '19 at 07:24
  • Yeah its working but that json values are dynamically created by html form. How to harcode that? – V. Monisha Jul 12 '19 at 07:33
  • Hardcoding was just for testing purpose to ensure of the error cause. To solve your requirement, handle the problem at the source : send a json object that contains the correct structure : array and not an array between quotes that is not an array but a string. – davidxxx Jul 12 '19 at 07:56
  • could you please provide me any example? – V. Monisha Jul 12 '19 at 07:59
  • Not really as it depends on your html. You should handle this point first. If really you don't manage create a new post with this specific issue and explain all what you did. – davidxxx Jul 12 '19 at 08:06
  • mapper.configure(DeserializationConfig.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true); This should resolve the issue – V. Monisha Jul 13 '19 at 02:02
  • It is right that it is a worarround to ease the task. Note that it doesn't respect the JSON standard concerning the array declaration. – davidxxx Jul 13 '19 at 07:07
0

Try below code,

public class Req {
    private List<String> no;
    private List<String> date;

    public List<String> getNo() {
        return no;
    }

    public void setNo(List<String> no) {
        this.no = no;
    }

    public List<String> getDate() {
        return date;
    }

    public void setDate(List<String> date) {
        this.date = date;
    }
}

Usage

Directly using controller method

@PostMapping("/test")
public ResponseEntity<?> test(@RequestBody Req req) {
    System.out.println(req.no);
}

Create object using Gson

Gson gson = new GsonBuilder().create();
Req req = gson.fromJson(yourjson, Req.class);

Convert String date to LocalDate

String date = "02/01/1991";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("d/MM/yyyy");
LocalDate d = LocalDate.parse(date, formatter);
Vimukthi
  • 846
  • 6
  • 19
  • 1
    1) A JSON is not necessarily exposed from a controller defined by the current code. That is not the actual requirement. JSON can come from local file, database, external service and so for... 2) Mapping dates to String is not the best thing to do. – davidxxx Jul 12 '19 at 06:19
  • You can't directly map this kind of strings to LocalDate objects. it will throw databind exceptions. – Vimukthi Jul 12 '19 at 07:50
0

You can map your object through Gson library.

YourObj obj = new Gson().fromJson(jsonString, YourObj.class);

Prepare a POJO class with no and name properties.

List<String> no;
List<String> date;

Gson is an open-source Java library to serialize and deserialize Java objects to JSON.

Googlian
  • 6,077
  • 3
  • 38
  • 44
  • I am using this code but it threw exception like com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was STRING at line 1 column 12 path $.no – V. Monisha Jul 12 '19 at 12:48