-1

I have stored an Json object in an String variable called output.

Assume,

String output;

This output variable is holding an json object.

below is the json object which output variable is holding.

How can access the price_currency which is in the prices?

"prices": [
      {
        "price_label": "",
        "price_currency": "USD",
        "price_wholesale": 32.00,
        "price_retail": 70.00,
        "price_currency_retail": "USD"
      }
  ],
  "deliveries": [{
      "delivery_name": "Zappos Holiday",
      "delivery_code": "",
      "style_display_order": 2,
      "season_name": "Holiday",
      "season_year" : "2017",
      "season_code": "",
      "date_cancel": "",
      "date_delivery_start": "",
      "date_delivery_end": "",
      "public": "0",
      "style_comments": ""
    },
Manu
  • 53
  • 2
  • 3
  • 8
  • Possible duplicate of [JSONObject - How to get a value ?](https://stackoverflow.com/questions/7451600/jsonobject-how-to-get-a-value) – Yamen Nassif May 24 '18 at 15:50
  • Possible duplicate of [How to parse JSON in Java](https://stackoverflow.com/questions/2591098/how-to-parse-json-in-java) – bhspencer May 24 '18 at 15:53
  • http://www.oracle.com/technetwork/articles/java/json-1973242.html – Jet_C May 24 '18 at 15:53

4 Answers4

2

There are many possibilities:

  • Converting the json in a bean (using libraries like Gson or Faster Jackson)
  • Converting the json in a Map (always using libraries like Gson or Faster Jackson)
  • Accessing directly the field you need using a regular expression (very complex)
  • Writing a parser
  • Using a library to access directly the field using Json path

Each of the previous possibilities has pro and cons.

For example if you need a particular field but you don't know the structure of the whole document you can use json path.

If you need to manage the whole json as an object to save it locally convert it to a bean.

And so on.

Davide Lorenzo MARINO
  • 26,420
  • 4
  • 39
  • 56
0

It is super simple, if you use the new javax.json package that was introduced in Java 7: https://docs.oracle.com/javaee/7/api/javax/json/package-summary.html

Example:

JsonReader jsonReader = Json.createReader(new StringReader(yourString));
JsonArray prices = jsonReader.readArray();
for (JsonValue price : prices){
    // ...
}
jsonReader.close();
Guilherme Mussi
  • 956
  • 7
  • 14
0

Firstly you said your response value is a string output, here is the steps of parsing a JSON object in JAVA.

  1. Parse your string value to JSONObject, let me name it jsonRet.
  2. Then parse this

jsonRet.getJSONArray("prices")

This step you also get a JSON array of prices let me call it prices

Foreach the prices array as JSONObject Then do price.getString("price_currency")

This step you get a string value, this would be what you want.

For more info, I suggest you search key word Java JSON with Google that would be benefit for your understanding.

-2

in java You have JSONObjet class which You can use in this case.

Some link to java doc --> click

plucins
  • 153
  • 2
  • 11