Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to split two json String

I have a one string but it contain two object like {"firstName":"A","lastName":"Z"}{"firstName":"B","lastName":"Y"}
i got this String as a response but i want to process it differantly one by one, so how i seprate this one like {"firstName":"A","lastName":"Z"} and {"firstName":"B","lastName":"Y"}Separatly

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Object mapper = new ObjectMapper();   
 List<YourClass> ls= ((ObjectMapper) mapper).readValue(
                    "[{"firstName":"A","lastName":"Z"},{"firstName":"B","lastName":"Y"}]",
                    new TypeReference<List<YourClass>>() {
                    });
//process list.

YourClass is mapping class of json input. Hope this is what you are looking for.

Edit:

As your input is received in {"firstName":"A","lastName":"Z"}{"firstName":"B","lastName":"Y"} fashion, then

String input=" {"firstName":"A","lastName":"Z"}{"firstName":"B","lastName":"Y"}";

String array[]=a.split("(?=\\{)|(?<=\\})");

System.out.println(array[0]);
System.out.println(array[1]);

Output:

{"firstName":"A","lastName":"Z"}
{"firstName":"B","lastName":"Y"}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading