0

If you pass multiple JSON arrays into Java (in a single stream) that looks like this:

[ <-----json objects---->] [ <-----json objects---->]  [ <-----json objects---->] 

How do you parse in Java? And is there a way to do it without rewriting the JSON data into a single array?

Essentialy, I want to get to a point where I can do this after the parsing.

item = json.getString("item");
total = json.getString("total");

items.add(item);
totals.add(total);

A key note is that the first array is items, the second array is totals. The first json object in items corresponds to the first in totals.

4
  • By single array, do you mean using the provided json library and pass the entire json object string to the constructor? You can then do what you want as it will parse it into name value pairs Commented Jul 31, 2012 at 18:32
  • would you have the entire stream at once? Commented Jul 31, 2012 at 18:32
  • Well I mean by enclosing the two arrays and make a single array out of it by adding "[" "]" around the two individual arrays. There was another old Stack question that used that as an answer, but I was looking for different method. Commented Jul 31, 2012 at 18:34
  • @Francisco Spaeth Yes, it is coming from a php encoding. Commented Jul 31, 2012 at 18:35

1 Answer 1

1

I would say that you need first of all transform this content in a valid JSON, maybe you could first replace ]\s*[ by ],[ and add [ at the beggining and ] at the end. This would be a valid JSON and could be parsed as an JSONArray that contains many JSONArrays.

Something like:

String receivedJSON = "[{},{}] [{},{}] [{}]";
String normalized = "[" + receivedJSON.replaceAll("\\]\\s*\\[", "],[") + "]";
new JSONArray(normalized);
Sign up to request clarification or add additional context in comments.

7 Comments

So it sounds like the method I am proposing is just not possible. They are both valid JSON arrays, you just can parse them simultaneously?
do you have control over the PHP script?
I do have access; I tried to play with it a while and actually have a corresponding question as well (it's asking a different thing than this though shows more fully what I am doing in the big picture). stackoverflow.com/questions/11744129/… (As you will see, I actually am outputting three arrays, but to simplify, used two for this example).
I see, you could think to adopt another model to represent what are you trying to do. When I need to give back a set of items like a search result I usually use something like: { "total":8, "offset":0, "items":[{},{},{},{}]} so you have the total, offset of the request page and the items all in one object
Well I fixed the issue by doing things right. I simply merged my 3 SQL queries into one. Therefore it was VERY easy to encode it as JSON without doing anything special.
|

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.