0

I received JSON object, and parsed it.

The data is as in the following.

[Parsed_showinfo_2 @ 0x9b4da80] n:88 pts:17425897 pts_time:726.079 pos:149422375 fmt:rgb24 sar:405/404 s:640x360 i:P iskey:1 type:I checksum:48E13810 plane_checksum:[48E13810]
[Parsed_showinfo_2 @ 0x9b4da80] n:89 pts:17471943 pts_time:727.998 pos:149646339 fmt:rgb24 sar:405/404 s:640x360 i:P iskey:0 type:P checksum:1AAD40E0 plane_checksum:[1AAD40E0]
[Parsed_showinfo_2 @ 0x9b4da80] n:90 pts:17503975 pts_time:729.332 pos:149806608 fmt:rgb24 sar:405/404 s:640x360 i:P iskey:1 type:I checksum:3DA5F0DB plane_checksum:[3DA5F0DB]

And then, I did parsing the data using JSONParser(), because I need pts, pts_time values.

FileInputStream fstream = new FileInputStream("myfile");
DataInputStream in = new DataInputStream(fstream);
BufferedReader buff = new BufferedReader(new InputStreamReader(in));
//Which type put to ?
HashMap<String, ?> map = new HashMap<String, ?>();
//Should I use ArrayList too?
//ArrayList<HashMap<String, ?>> list = new ArrayList<HashMap<String, ?>>();
try {
 while (strLine = buff.readLine()) != null) {
 s = strLine.split(" ");
 pts = Long.parseLong(s[4].split(":")[1]);
 ptstime = s[5].split(":")[1];
 }
} catch (IOException ex) { }

I want make multiple array in the while-loop, and return result value.

Like this:

["pts": {1, 2, 3, 4, 5}, "ptstime": {10.11, 13.003, 15.12, 16.53, 18.10}]

The pts and ptstime are the keys.

How can I coding in the while-loop

Vampire
39.1k4 gold badges83 silver badges110 bronze badges
asked May 12, 2016 at 10:12
2
  • Where is the JSON object? Where do you call the JSON parser? What are you reading from the file? And why via DataInputStream? Commented May 12, 2016 at 10:17
  • you can just add the values into the hash map after the loop, Commented May 12, 2016 at 10:18

2 Answers 2

1

Here is how you can do it:

Map<String, List<Object>> map = new HashMap<>();
...
while (strLine = buff.readLine()) != null) {
 s = strLine.split(" ");
 pts = Long.parseLong(s[4].split(":")[1]);
 List<String> listPts = map.get("pts");
 if (listPts == null) {
 listPts = new ArrayList<>();
 map.put("pts", listPts);
 }
 listPts.add(pts);
 // Here apply the same logic to the rest
}

But a better approach could be to use a List of Map instead, like this:

List<Map<String, Object>> list = new ArrayList<>();
while (strLine = buff.readLine()) != null) {
 s = strLine.split(" ");
 Map<String, String> map = new HashMap<>();
 list.add(map);
 pts = Long.parseLong(s[4].split(":")[1]);
 map.put("pts", pts);
 // Here apply the same logic to the rest
}
answered May 12, 2016 at 10:29
0
1

What is the DataInputStream for as you use it just as argument for the InputStreamReader, the FileInputStream should be fine already.

As to your question, I guess what you want is

Map<String, List<?>> map = new HashMap<>(2);
map.put("pts", new ArrayList<Long>());
map.put("ptstime", new ArrayList<Long>());

And then in the while loop something like

map.get("pts").add(pts.toString());
map.get("ptstime").add(ptstime);
answered May 12, 2016 at 10:32

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.