Java 如何从这个JSONString解析?
我通过HTTP请求Java 如何从这个JSONString解析?,java,android,json,http,Java,Android,Json,Http,我通过HTTP请求JSONString获得下面的JSONString。我想要这个字符串中的所有amount值 我已经试过了: try { // Getting JSON Array JSONArray spritPriceArr = jObj.getJSONArray("spritPrice"); int spritPriceArrLength = spritPriceArr.length(); for(int i=0; i < spritPriceArr
JSONString
获得下面的JSONString
。我想要这个字符串中的所有amount
值
我已经试过了:
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();
}
试试看{
//获取JSON数组
JSONArray spritpricer=jObj.getJSONArray(“spritPrice”);
int spritPriceArrLength=spritPriceArr.length();
for(int i=0;i
这适用于第一个spritPrice数组
。但是如何获得另一个spritPrice数组的数量
我真的不知道如何做到这一点。谁能帮帮我吗
这是我完整的JSON字符串的链接:“,”serviceText:“,”maestro:”true,“companyship:”false,“address:”Hofer Straße1(”Hofer Parkplatz“)“,”club:”false,“errorCode:”1,“service:”false,“latitude:”48.244911“,”bar:”false,“telephone:”4362467223634“}]不只是获取字符串,还获取ArrayList或List并添加每个价格 像这样,
ArrayList<String> prices=new ArrayList<String>
您可以询问是否有任何进一步的查询:)这是我从jsonarray获取字符串的方式,请尝试这样做:
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();
}
试试看{
JSONArray spritpricer=jObj.getJSONArray(“spritPrice”);
int spritPriceArrLength=spritPriceArr.length();
字符串[]StringofAmount=新字符串[spritPriceArrLength]
for(int i=0;i
试试这个
// 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();
}
//ArrayList amountList=new ArrayList();
字符串金额=空;
试一试{
//获取JSON数组
字符串您的_json_响应;
JSONArray responseArray=新的JSONArray(您的json响应);
对于(int j=0;j这可能会帮助您
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
}
}
private ArrayList ArrayList=new ArrayList();
public void loadAmountFrom(字符串jsonArrayString){
试一试{
JSONArray JSONArray=新的JSONArray(jsonArrayString);
jsonArray的装载量(jsonArray);
}捕获(例外e){
//TODO:处理异常
}
}
JSONArray(JSONArray-JSONArray)的专用void LoadAmount{
试一试{
for(int i=0;i
只需将字符串响应传递给loadAmountFrom(String)方法…您的数组spritPrice
的长度为1:
"spritPrice":[
{
"amount":"1.329",
"datAnounce":"2013-11-27 13:05:07",
"errorItems":[
],
"errorCode":0,
"datValid":1385553907000,
"spritId":"DIE"
}
],
根元素必须是JSONArray
,而不是JSONObject
一个好的链接:
它表明您的JSON是一个数组
[
{
"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"
}
]
这会奏效的
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();}
试试看{
JSONArray json=新JSONArray(str);
for(int i=0;i
我只能给你一个如何做的参考。是的,请。如果你发布了错误的JSON响应,那就太好了。更新你的JSON字符串检查本教程的参考资料否,这是我真正得到的。它只是一个更长的字符串,有更多的spritprice。但就是这样。它不会改变任何东西。稍后你可以从ArrayList访问所有价格。对于e.g String price1=prices.get(0);String price2=prices.get(1);但它仍然是第一个价格以后您可以从ArrayList访问所有价格。例如,String price1=prices.get(0);String price2=prices.get(1);没有。只有第一个价格。循环结束于第一个spritPrice
它给了我第一个价格的10倍。很好。我改变了一些东西。现在它工作了。我编辑了你的帖子,然后我接受了它。
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();}