Search code examples
javaarraysjsonint

How to pull an Int array from JSONOBJECT? Java


Tell me, please, how can I read an array of integers from the item "compatibility"?

{
    "id": 3,
    "text": "Some text",
    "action": 1,
    "compatibility": [ 4, 5 ]
},

Thank you in advance!


Solution

  • 
    
        JsonObject value = Json.createObjectBuilder()
            .add("id", 3)
            .add("text", "Some text")
            .add("action", 1)
            .add("phoneNumber", 
                    Json.createArrayBuilder()
                    .add(4)
                    .add(5)
                    .add(6)
        ).build();
        JsonArray array =  value.get("phoneNumber").asJsonArray();
        array.forEach( a -> {
            System.out.println( "int " +  a );      
        } );
    

    from here you can already operate the integers according to the need