I get the below JSONString
through HTTP Request
. And I want all amount
values from this String.
I tried this so far:
try {
// Getting JSON Array
JSONArray spritPriceArr = jObj.getJSONArray("spritPrice");
int spritPriceArrLength = spritPriceArr.length();
for(int i=0; i < spritPriceArrLength; i++ ){
JSONObject c = spritPriceArr.getJSONObject(i);
String amount = c.getString("amount");
System.out.println(amount);
}
} catch (JSONException e) {
e.printStackTrace();
}
This works for the first spritPrice Array
. But how can I get the amounts
of the other spritPrice Array
?
I really dont know how to achieve this. Can anyone help me please.
This is a link to my full JSON String: >>This<<
[{"kredit":true,"self":false, "spritPrice" :[{ "amount" :"1.329","datAnounce":"2013-11-27 13:05:07","errorItems":[],"errorCode":0,"datValid":1385553907000,"spritId":"DIE"}],"automat":true,"city":"Braunau","open":true,"distance":2.21,"postalCode":"5280","errorItems":[],"priceSearchDisabled":false,"longitude":"13.0365064","payMethod":"Routex","mail":"","gasStationName":"BP","fax":"","clubCard":"","openingHours":[{"beginn":"06:00","day":{"dayLabel":"Sonntag","order":7,"errorItems":[],"errorCode":0,"day":"SO"},"end":"22:00"},{"beginn":"06:00","day":{"dayLabel":"Mittwoch","order":3,"errorItems":[],"errorCode":0,"day":"MI"},"end":"22:00"},{"beginn":"06:00","day":{"dayLabel":"Feiertag","order":8,"errorItems":[],"errorCode":0,"day":"FE"},"end":"22:00"},{"beginn":"06:00","day":{"dayLabel":"Montag","order":1,"errorItems":[],"errorCode":0,"day":"MO"},"end":"22:00"},{"beginn":"06:00","day":{"dayLabel":"Donnerstag","order":4,"errorItems":[],"errorCode":0,"day":"DO"},"end":"22:00"},{"beginn":"06:00" ,"day":{"dayLabel":"Freitag","order":5,"errorItems":[],"errorCode":0,"day":"FR"},"end":"22:00"},{"beginn":"06:00","day":{"dayLabel":"Samstag","order":6,"errorItems":[],"errorCode":0,"day":"SA"},"end":"22:00"},{"beginn":"06:00","day":{"dayLabel":"Dienstag","order":2,"errorItems":[],"errorCode":0,"day":"DI"},"end":"22:00"}],"access":"","url":"","serviceText":"gratis Staubsaugen","maestro":true,"companionship":false,"address":"Salzburger Str. 11","club":false,"errorCode":1,"service":false,"latitude":"48.2546591","bar":true,"telephone":""},{"kredit":true,"self":false, "spritPrice" :[{ "amount ":"1.334","datAnounce":"2013-11-27 12:40:18","errorItems":[],"errorCode":0,"datValid":1385552418000,"spritId":"DIE"}],"automat":true,"city":"Braunau","open":true,"distance":2.42,"postalCode":"5280","errorItems":[],"priceSearchDisabled":false,"longitude":"13.048711","payMethod":"Novofleet","mail":"office@fetrading.at","gasStationName":"FE-Trading GmbH ","fax":"4362467223611","clubCard":"","openingHours":[{"beginn":"06:00","day":{"dayLabel":"Montag","order":1,"errorItems":[],"errorCode":0,"day":"MO"},"end":"21:00"},{"beginn":"06:00","day":{"dayLabel":"Donnerstag","order":4,"errorItems":[],"errorCode":0,"day":"DO"},"end":"21:00"},{"beginn":"06:00","day":{"dayLabel":"Dienstag","order":2,"errorItems":[],"errorCode":0,"day":"DI"},"end":"21:00"},{"beginn":"06:00","day":{"dayLabel":"Samstag","order":6,"errorItems":[],"errorCode":0,"day":"SA"}," end":"21:00"},{"beginn":"08:00","day":{"dayLabel":"Sonntag","order":7,"errorItems":[],"errorCode":0,"day":"SO"},"end":"18:00"},{"beginn":"08:00","day":{"dayLabel":"Feiertag","order":8,"errorItems":[],"errorCode":0,"day":"FE"},"end":"18:00"},{"beginn":"06:00","day":{"dayLabel":"Freitag","order":5,"errorItems":[],"errorCode":0,"day":"FR"},"end":"21:00"},{"beginn":"06:00","day":{"dayLabel":"Mittwoch","order":3,"errorItems":[],"errorCode":0,"day":"MI"},"end":"21:00"}],"access":"","url":" http://www.fe-trading.at ","serviceText":"","maestro":true,"companionship":false,"address":"Hofer Straße 1 (\\" Hofer Parkplatz\\")","club":false,"errorCode":1,"service":false,"latitude":"48.244911","bar":false,"telephone":"4362467223634"}]
This will work.
try{
JSONArray json = new JSONArray(str);
for(int i = 0; i< json.length();i++)
{
JSONObject c1 = (JSONObject) json.get(i);
JSONArray spritPrice = c1.getJSONArray("spritPrice");
for (int j = 0; j < spritPrice.length(); j++)
{
JSONObject c2 = (JSONObject) spritPrice.get(j);
String amount = c2.getString("amount");
System.out.println(amount);
}
}}catch (JSONException e){
e.printStackTrace();}
Instead of just taking String,also take ArrayList or List and add each price.
Like this,
ArrayList<String> prices=new ArrayList<String>
Write below code inside your loop.
String amount = c.getString("amount");
prices.add(amount);
You can ask if you have any further queries :)
This is how I get strings from jsonarray, try to do it:
try {
JSONArray spritPriceArr = jObj.getJSONArray("spritPrice");
int spritPriceArrLength = spritPriceArr.length();
String[] StringofAmount = new Strng[spritPriceArrLength]
for(int i=0; i < spritPriceArrLength; i++ ){
JSONObject c = spritPriceArr.getJSONObject(i);
StringofAmount[i] = c.optString("amount");
}
} catch (JSONException e) {
e.printStackTrace();
}
Try this,
// ArrayList<String> amountList=new ArrayList<String>();
String amount=null;
try {
// Getting JSON Array
String your_json_response;
JSONArray responseArray = new JSONArray(your_json_response);
for(int j=0;j<responseArray.length();j++)
{
JSONArray spritPrice=responseArray.getJSONObject(i).JSONArray("spritPrice");
int spritPrice= spritPrice.length();
for(int i=0; i < spritPrice; i++ ){
JSONObject c = spritPrice.getJSONObject(i);
amount = c.getString("amount");
// amountList.add(amount);
System.out.println(amount);
if(amount!=null)
break;
}
if(amount!=null)
break;
}
} catch (JSONException e) {
e.printStackTrace();
}
this may help you...
private ArrayList<String> arrayList = new ArrayList<String>();
public void loadAmountFrom(String jsonArrayString) {
try {
JSONArray jsonArray = new JSONArray(jsonArrayString);
loadAmountFromJsonArray(jsonArray);
} catch (Exception e) {
// TODO: handle exception
}
}
private void loadAmountFromJsonArray(JSONArray jsonArray) {
try {
for (int i = 0; i < jsonArray.length(); i++) {
Object object = jsonArray.get(i);
if (object instanceof JSONArray) {
loadAmountFromJsonArray(jsonArray);
} else if (object instanceof JSONObject) {
JSONObject jsonObject = (JSONObject) object;
loadAmount(jsonObject);
}
}
} catch (Exception e) {
// TODO: handle exception
}
}
private void loadAmount(JSONObject jsonObject) {
try {
Iterator<String> iterator = jsonObject.keys();
while(iterator.hasNext()) {
String key = (String) iterator.next();
if(key.equals("amount")) {
Object object = jsonObject.get(key);
arrayList.add(object.toString());
} else {
Object object = jsonObject.get(key);
if(object instanceof JSONArray) {
loadAmountFromJsonArray((JSONArray) object);
} else if(object instanceof JSONObject) {
loadAmount((JSONObject) object);
}
}
}
} catch (Exception e) {
// TODO: handle exception
}
}
just pass String response to loadAmountFrom(String) method...
Your array spritPrice
is of length 1:
"spritPrice":[
{
"amount":"1.329",
"datAnounce":"2013-11-27 13:05:07",
"errorItems":[
],
"errorCode":0,
"datValid":1385553907000,
"spritId":"DIE"
}
],
Your root element must be JSONArray
, not JSONObject
.
A good link: http://jsonformatter.curiousconcept.com/
It shows that your JSON is an array.
[
{
"kredit":true,
"self":false,
"spritPrice":[
{
"amount":"1.329",
"datAnounce":"2013-11-27 13:05:07",
"errorItems":[
],
"errorCode":0,
"datValid":1385553907000,
"spritId":"DIE"
}
],
"automat":true,
"city":"Braunau",
"open":true,
"distance":2.21,
"postalCode":"5280",
"errorItems":[
],
"priceSearchDisabled":false,
"longitude":"13.0365064",
"payMethod":"Routex",
"mail":"",
"gasStationName":"BP",
"fax":"",
"clubCard":"",
"openingHours":[
{
"beginn":"06:00",
"day":{
"dayLabel":"Sonntag",
"order":7,
"errorItems":[
],
"errorCode":0,
"day":"SO"
},
"end":"22:00"
},
{
"beginn":"06:00",
"day":{
"dayLabel":"Mittwoch",
"order":3,
"errorItems":[
],
"errorCode":0,
"day":"MI"
},
"end":"22:00"
},
{
"beginn":"06:00",
"day":{
"dayLabel":"Feiertag",
"order":8,
"errorItems":[
],
"errorCode":0,
"day":"FE"
},
"end":"22:00"
},
{
"beginn":"06:00",
"day":{
"dayLabel":"Montag",
"order":1,
"errorItems":[
],
"errorCode":0,
"day":"MO"
},
"end":"22:00"
},
{
"beginn":"06:00",
"day":{
"dayLabel":"Donnerstag",
"order":4,
"errorItems":[
],
"errorCode":0,
"day":"DO"
},
"end":"22:00"
},
{
"beginn":"06:00",
"day":{
"dayLabel":"Freitag",
"order":5,
"errorItems":[
],
"errorCode":0,
"day":"FR"
},
"end":"22:00"
},
{
"beginn":"06:00",
"day":{
"dayLabel":"Samstag",
"order":6,
"errorItems":[
],
"errorCode":0,
"day":"SA"
},
"end":"22:00"
},
{
"beginn":"06:00",
"day":{
"dayLabel":"Dienstag",
"order":2,
"errorItems":[
],
"errorCode":0,
"day":"DI"
},
"end":"22:00"
}
],
"access":"",
"url":"",
"serviceText":"gratis Staubsaugen",
"maestro":true,
"companionship":false,
"address":"Salzburger Str. 11",
"club":false,
"errorCode":1,
"service":false,
"latitude":"48.2546591",
"bar":true,
"telephone":""
},
{
"kredit":true,
"self":false,
"spritPrice":[
{
"amount":"1.334",
"datAnounce":"2013-11-27 12:40:18",
"errorItems":[
],
"errorCode":0,
"datValid":1385552418000,
"spritId":"DIE"
}
],
"automat":true,
"city":"Braunau",
"open":true,
"distance":2.42,
"postalCode":"5280",
"errorItems":[
],
"priceSearchDisabled":false,
"longitude":"13.048711",
"payMethod":"Novofleet",
"mail":"office@fetrading.at",
"gasStationName":"FE-Trading GmbH ",
"fax":"4362467223611",
"clubCard":"",
"openingHours":[
{
"beginn":"06:00",
"day":{
"dayLabel":"Montag",
"order":1,
"errorItems":[
],
"errorCode":0,
"day":"MO"
},
"end":"21:00"
},
{
"beginn":"06:00",
"day":{
"dayLabel":"Donnerstag",
"order":4,
"errorItems":[
],
"errorCode":0,
"day":"DO"
},
"end":"21:00"
},
{
"beginn":"06:00",
"day":{
"dayLabel":"Dienstag",
"order":2,
"errorItems":[
],
"errorCode":0,
"day":"DI"
},
"end":"21:00"
},
{
"beginn":"06:00",
"day":{
"dayLabel":"Samstag",
"order":6,
"errorItems":[
],
"errorCode":0,
"day":"SA"
},
"end":"21:00"
},
{
"beginn":"08:00",
"day":{
"dayLabel":"Sonntag",
"order":7,
"errorItems":[
],
"errorCode":0,
"day":"SO"
},
"end":"18:00"
},
{
"beginn":"08:00",
"day":{
"dayLabel":"Feiertag",
"order":8,
"errorItems":[
],
"errorCode":0,
"day":"FE"
},
"end":"18:00"
},
{
"beginn":"06:00",
"day":{
"dayLabel":"Freitag",
"order":5,
"errorItems":[
],
"errorCode":0,
"day":"FR"
},
"end":"21:00"
},
{
"beginn":"06:00",
"day":{
"dayLabel":"Mittwoch",
"order":3,
"errorItems":[
],
"errorCode":0,
"day":"MI"
},
"end":"21:00"
}
],
"access":"",
"url":"http://www.fe-trading.at",
"serviceText":"",
"maestro":true,
"companionship":false,
"address":"Hofer Straße 1 (\" Hofer Parkplatz\")",
"club":false,
"errorCode":1,
"service":false,
"latitude":"48.244911",
"bar":false,
"telephone":"4362467223634"
}
]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.