I have a string but it contains two object like the following {"firstName":"A","lastName":"Z"}{"firstName":"B","lastName":"Y"}
I got this string as a response but I want to process it one by one, so how I should separate this one like {"firstName":"A","lastName":"Z"}
and {"firstName":"B","lastName":"Y"}
Asked
Active
Viewed 606 times
-2

Guhan
- 31
- 1
- 7

pratikpoponimb
- 3
- 5
-
2How do you get the strings? Are they separated by a line break or a similar delimiter? Do you get one string per line? – jabaa May 20 '22 at 08:04
-
Also, javascript or java? They are not the same. – Chaosfire May 20 '22 at 08:07
-
i get this string as response in one line {"firstName":"A","lastName":"Z"}{"firstName":"B","lastName":"Y"} and in Java – pratikpoponimb May 20 '22 at 08:11
-
1@pratikpoponimb , is it not json array? – Ashish Patil May 20 '22 at 08:14
-
Does this answer your question? [How to split a string in Java](https://stackoverflow.com/questions/3481828/how-to-split-a-string-in-java) – pringi May 20 '22 at 08:19
-
@pratikpoponimb , updated my answer, please refer below – Ashish Patil May 20 '22 at 08:23
-
1It's not valid JSON. It's just a string that _resembles_ JSON. – Andy May 20 '22 at 08:25
-
@Andy it just string in json – pratikpoponimb May 20 '22 at 08:29
-
1Valid JSON would be `[{"firstName":"A","lastName":"Z"},{"firstName":"B","lastName":"Y"}]`. – Andy May 20 '22 at 08:30
-
@pratikpoponimb - this is not json – Scary Wombat May 20 '22 at 08:32
-
@Andy Yes you are right but i got response from server {"firstName":"A","lastName":"Z"}{"firstName":"B","lastName":"Y"} – pratikpoponimb May 20 '22 at 08:34
-
@ScaryWombat Yes – pratikpoponimb May 20 '22 at 08:42
-
Essentially you just need to split the string, and process each segment as if it were a valid JSON object. What you have isn't valid as an object or an array. One way to do it is split the string on "}{" which isn't a valid JSON sequence, and shouldn't appear anywhere else. Assuming there are no other new lines in the string, something like this should do the job... `String[] objects = inputString.replaceAll("\\}\\{","\\}\n\\{").split("\n");` – bigtlb May 21 '22 at 18:53
1 Answers
1
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[] = input.split("(?=\\{)|(?<=\\})");
System.out.println(array[0]);
System.out.println(array[1]);
Output:
{"firstName":"A","lastName":"Z"}
{"firstName":"B","lastName":"Y"}

user16320675
- 135
- 1
- 3
- 9

Ashish Patil
- 4,428
- 1
- 15
- 36
-
in array[] i got [{"firstName":"A","lastName":"Z"}, , {"firstName":"B","lastName":"Y"}] in array[0] i got {"firstName":"A","lastName":"Z"} and array[1] i got blank and array[2] i got {"firstName":"B","lastName":"Y"} – pratikpoponimb May 20 '22 at 08:38
-
Because you have extra `, ` in your input, can you please see what happens without it? – Ashish Patil May 20 '22 at 08:43
-
-
if i want array[1]= {"firstName":"B","lastName":"Y"} for that what should i do – pratikpoponimb May 20 '22 at 09:41
-
It means, your input is quite malformed & you have extra blank `,` within it. like `{"firstName":"A","lastName":"Z"}, , {"firstName":"B","lastName":"Y"} `. If you are receiving such malformed input, then that would be problem & you will receive even some other characters in between. Best you can do is ignore if array's element is blank & move on to next element. – Ashish Patil May 20 '22 at 09:45