JSON RECORD IT

1

I have this JSON

{
    "id":2,
    "group_id":1,
    "default_billing":"2",
    "default_shipping":"2",
    "created_at":"2018-10-03 16:12:55",
    "updated_at":"2018-10-03 16:13:00",
    "created_in":"Default Store View",
    "email":"[email protected]",
    "firstname":"Henry",
    "lastname":"Arcila",
    "store_id":1,
    "website_id":1,
        "addresses":[{
            "id":2,
            "customer_id":2,
                "region":{
                    "region_code":"NY",
                    "region":"New York",
                    "region_id":43
                    },
                "region_id":43,
                "country_id":"US",
                    "street":[
                                "123 Oak Ave"
                            ],
                "telephone":"512-555-1111",
                "postcode":"10755",
                "city":"Purchase",
                "firstname":"Henry",
                "lastname":"Arcila",
                "default_shipping":true,
                "default_billing":true
        }],
        "disable_auto_group_change":0
}

That JSON I got from this line of code:

JSONObject json = new JSONObject(responseStrBuilder.toString());

Since I am extracting this from a REST query, I want to save that JSON in a variable and then extract each one of these fields that are inside the ADDRESSES :

  

firstname, lastname, region_code, region, region_id, country_id,   street, telephone, postcode and city.

At the time of obtaining them I want to save them in different variables and have them stored because then in other REST responses I need to load another JSONObject those variables, an example is this:

JSONObject JshippingAddress = new JSONObject();
                JshippingAddress.put("email", "[email protected]");
                JshippingAddress.put("region", "New York");
                JshippingAddress.put("region_id", 43);
                JshippingAddress.put("region_code", "NY");
                JshippingAddress.put("country_id", "US");
                JshippingAddress.put("street", JCalle);
                JshippingAddress.put("postcode", "10577");
                JshippingAddress.put("city", "Purchase");
                JshippingAddress.put("telephone", "512-555-111");
                JshippingAddress.put("firstname", "Henry");
                JshippingAddress.put("lastname", "Arcila");

At the moment they were HARD CODE, it is worth noting that they are in different classes so I keep GLOBAL variables in a controller.

    
asked by Andrea Valentina 06.12.2018 в 18:18
source

2 answers

0

First let's see that addresses is an array within JSONObject then to access it:

JSONArray jsonAdress = json.getJSONArray("addresses");

But that array brings 13 JSONObject inside so you have to go through it to access each one.

    
answered by 07.12.2018 / 19:05
source
1

You can use JSONObject to handle the json as an object and thus be able to take the value of any of its fields:

JSONObject miJson = new JSONObject(<tuJSON>); //JSONObject (String json)
String field = miJson.get("fieldName").toString()); //Object get (String name)
    
answered by 06.12.2018 в 18:27