-3

I have a problem with JSON; I can not read file from portblock but webblock can be done

This is test.json

{
    "webblock" : ["www.google.com", "www.youtube.com", "www.facebook.com"],
    "portblock" : [{
            "key" : "80",
            "value" : "tcp"
        }, {
            "key" : "70",
            "value" : "udp"
        }
    ]
}

and this is code I used display webblock.

JSONArray msg = (JSONArray) jsonObject.get("webblock");
Iterator<String> iterator = msg.iterator();
while (iterator.hasNext()) {
    System.out.println(iterator.next());
    block_web(iterator.next());
}

Help me to display key and value of portblock.

Pshemo
  • 122,468
  • 25
  • 185
  • 269
OhwMaI
  • 13
  • 3
  • 4
    So what exactly is the problem? – Mureinik Oct 08 '16 at 11:14
  • You are calling `next()` twice in your loop, which means you are getting separate values for each call. Store result of `next()` in String variable and use it instead of `iterator.next()`. – Pshemo Oct 08 '16 at 11:25

1 Answers1

2

Take a look a Jackson. This code should work:

  1. Create the Pojo

    public class DomainName {
    
         List<String> webblock;
         List<PortBlock> portblock;
         //getter and setters
    }
    
    public class PortBlock{
         Integer port;
         String value;
         //getter and setters
    }
    
  2. Map to the Domain Class

    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.readValue(YOUR_JSON_STRING, DomainName.class)
    
Simon Ludwig
  • 1,754
  • 1
  • 20
  • 27