1

I am having the below JSON. Inside this JSON I am having "ticketDetails" as JSON array. From this array I want to retrieve the value of ticketPrice inside the json object "amount". How can I do that?

{
    "ticketDetails": [{
        "seq": 1,
        "qty": 2,
        "amount": {
            "ticketPrice": 120,
            "bookingFee": 50
        },
        "session": {
            "id": 1001,
            "date": "2013, 9, 15",
            "time": "1300"
        },
        "venue": {
            "id": "MTRG",
            "name": "Adlabs Manipur",
            "companyCode": "ADLB"
        },
        "event": {
            "id": "ET00000001123",
            "name": "Chennai Express",
            "producerCode": "YRF"
        },
        "category": {
            "ttypeCode": "00012",
            "areaCatCode": "2414",
            "type": "Gold",
            "price": 270
        }
    }]
}

Any suggestion will helpful...

2
  • 2
    Which programming language do you use? JavaScript? By the way, the enter code here makes your JSON invalid. Commented Oct 21, 2013 at 7:56
  • Is there a reason ticketDetails is an array? If you want an extremely modular, object oriented solution, you could check out the GSON library. It may be overkill, but it's worth a quick gander. You would essentially deserialize the json into its respective java objects and then use getters to retrieve the data you want. Commented Oct 22, 2013 at 5:47

3 Answers 3

2

Below is the sample code for retrieving the ticketPrice from the given JSONObject:

JSONObject objData = (JSONObject)JSONSerializer.toJSON(data);
JSONArray objTicketDetailsJsonArr = objData.getJSONArray("ticketDetails");
for(int nSize=0; nSize < objTicketDetailsJsonArr.size(); nSize++){
    String ticketPrice = "";
    ticketPrice = objTicketDetailsJsonArr.getString("ticketPrice");
}

Below are the imports for the above code:

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JSONSerializer;

source of JAR: http://json-lib.sourceforge.net/

Sign up to request clarification or add additional context in comments.

Comments

1

you store your data within a variable

  data = {...}

then you access it this way:

data.ticketDetails[0].amount.ticketPrice 

if the ticketDetails have more than one element

you can loop over the ticketDetails array and store all the ticketPrice values within an other array, ticketPriceArray

the following would work fine in JavaScript:

var ticketPriceArray = data.ticketDetails.map(function(k){
 return k.amount.ticketPrice;
});

if you are using another programming language a general loop would work fine also

for ( i; i< ticketDetails.length ; i++){
    ticketPriceArray[i] = data.ticketDetails.amount.ticketPrice[i];
}

For Java check this tutorial: http://answers.oreilly.com/topic/257-how-to-parse-json-in-java/

Comments

0

you can try this code:

JsonObject transactiondata = (JsonObject)Offer.get("transData");
JsonObject ticketdata = (JsonObject)transactiondata.get("tickets");
JsonObject offerData = (JsonObject)Offer.get("offerData");
JsonObject offerData1 = (JsonObject)offerData.get("offerconfig");

JsonArray jsonarr= (JsonArray)ticketdata.get("ticketDetails");

double ticketPrice = Double.parseDouble(jsonarr.get(0).getAsJsonObject().get("amount").getAsJsonObject().get("ticketPrice").getAsString());
System.out.println("ticketPrice:"+ticketPrice);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.