简体   繁体   中英

How can I convert Json String to List<HashMap<String, String>>?

I creating compare data A and each data using Java.

First, I did extract array data from txt file (array type in file).

Second, I have a Json String data in my database (MySQL column type : JSON).

I parsed txt file and make List

FileInputStream fstream = new FileInputStream(Path);
List<HashMap<String, String>> list = list(fstream);

public List<HashMap<String, String>> list(FileInputStream fstream) {
  BufferedReader buff = new BufferedReader(new InputStreamReader(fstream));
  List<HashMap<String, String> list = new ArrayList<>();

  try {
    while ((strLine = buff.readLine()) != null) {
      s = strLine.split(" ");
      HashMap<String, String> map = new HashMap<>();
      pts = s[4].split(":")[1];
      ptstime = s[5].split(":")[1];
      map.put("pts", pts);
      map.put("ptstime", getDurationString(ptstime));
      if (pts_itv.equals("0") && ptstime_itv.equals("00:00")) {
        map.put("pts_itv", "0");
        map.put("ptstime_itv", "00:00");
      }
      else {
        map.put("pts_itv", String.valueOf(Long.parseLong(pts) - Long.parseLong(pts_itv)));
        map.put("ptstime_itv", getDurationString(String.valueOf(String.format("%.4f", Double.parseDouble(ptstime) - Double.parseDouble(ptstime_itv)))));
      }
      pts_itv = pts;
      ptstime_itv = ptstime;
      list.add(map);
    }
  }
  catch (IOException ex) {
        LOG.error("ERROR : " + ex.getLocalizedMessage() + ", " + ex.getMessage());
  }
  finally {
    try {
      buff.close();
    } catch (IOException ex) {}
  }
  return list;
}

Extraction data

[{pts_itv=0, ptstime=00:00:00.112792, pts=2707, ptstime_itv=00:00}, {pts_itv=192192, ptstime=00:00:08.12079, pts=194899, ptstime_itv=00:00:08.80}, {pts_itv=128128, ptstime=00:00:13.4595, pts=323027, ptstime_itv=00:00:05.3387}, {pts_itv=277277, ptstime=00:00:25.127, pts=600304, ptstime_itv=00:00:11.5532}]

I can get index key, object key.

list.get(0).get("pts_itv");

And Second data (SELECT Query from MySQL (Column JSON Type))

rs = Web.getInstance().getList(idx); //get data by sql query
jObj = (JSONObject) new JSONParser().parse(rs);
jArr = (JSONArray) jObj.get("data");
for (int i = 0; i < jArr.size(); i++) {
  JSONObject Jarr = (JSONObject) jArr.get(i);
  System.out.println(Jarr.get("PtsData"));
}

PtsData is

[{"pts": "81831", "pts_itv": "0", "ptstime": "00:00:03.40963", "ptstime_itv": "00:00"}, {"pts": "127877", "pts_itv": "46046", "ptstime": "00:00:05.32821", "ptstime_itv": "00:00:01.9186"}, {"pts": "157907", "pts_itv": "30030", "ptstime": "00:00:06.57946", "ptstime_itv": "00:00:01.2512"}]
java.lang.String

I want PtsData convert to first data type. How can I convert PtsData to List type?

Here are some suggestions to solve this problem.

  1. Don't use String operations un-till they are really needed, you can avoid split by using JSON library to parse JSON data. More code you write, more bugs you introduce and more maintenance it need.
  2. Use Object modelling. This would be more readable to use it in HashMap. Create an object model by defining peroperties pts, pts_itv, ptstime, ptstime_itv.Then choose your key and value. Define equals and hashcode. Then check equality of objects either with your own way to or use equals.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM