Home » Java » json – Scraping Data from the List java-Exceptionshub

json – Scraping Data from the List java-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment


I have a list which looks like below:

  StartingNmap7.60(https: //nmap.org)at2020-02-1713: 32IST,
  Nmapdone: 256IPaddresses(5hostsup)scannedin21.02seconds

It looks like messy. I want to get some data from that list.I want the required output to be like below:

[{"host name":"","IP address":"},
{"host name":"bevywise_37e0","IP address:""},
{"host name":"shiv-thinkpad-t420","IP address":""},
{"host name":"vivo-1726","IP address":""},
{"host name":"realme-5","IP address":""}]

Since I am new to java, I can’t figure out how to make it. Help me with some solutions.

How to&Answers:

If this list is list/array of strings you can do the folowing

  1. Filter your list by string prefix, in your case it will be

    String prefix = "Nmapscanreportfor";
  2. Create formating template where you will put parsed data

    String template = “{\”host name\”:\”%s\”,\”IP address\”:%s\”}”;

  3. Then iterate over your list and parse host name and ip address. String.indexOf() will be usefull. E.g. to get hostname, you can do

    listEntry.substring(prefix.length(), entry.indexOf("("))

    For ip it wil be similar, just find first “(” and last “)”. String between is your ip address.

  4. And when you will have hostname and ip address you can use String.format() method to fill template string placeholders.

To iterate over the list you can use for loop or streams. With streams it will look pretty nice

        List<String> newList = Arrays.stream(array) //if it's array of list.stream() if it's list
            .filter(entry -> entry.startsWith(prefix))
            .map(entry -> String.format(template, %your hostname%, %your ip address%))