如何解析这个JSONString?

时间:2013-11-27 12:41:23

标签: java android json http

我通过JSONString获得了以下HTTP Request。我想要来自这个String的所有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();
    }

这适用于第一个spritPrice Array。但是,我怎样才能获得另一个amounts的{​​{1}}?

我真的不知道如何实现这一目标。任何人都可以帮助我。

这是我的完整JSON字符串的链接:>>This<<

[{ “kredit”:真, “自我”:假,的 “spritPrice”:[{的 “量”: “1.329”, “datAnounce”:” 2013-11-27 13:05:07“,”errorItems“:[],”errorCode“:0,”datValid“:1385553907000,”spritId“:”DIE“}],”automat“:true,”city“ : “布劳瑙”, “打开”:真, “距离”:2.21, “POSTALCODE”: “5280”, “errorItems”:[], “priceSearchDisabled”:假, “经度”: “13.0365064”, “payMethod”: “Routex”, “邮件”: “”, “gasStationName”: “BP”, “传真”: “”, “会员卡”: “”, “openingHours”:[{ “初学者”: “06:00”,”天 “:{” dayLabel “:” 桑塔格”, “命令”:7, “errorItems”:[] “的errorCode”:0, “天”: “SO”}, “结束”: “22:00”} ,{ “初学者”: “06:00”, “天”:{ “dayLabel”: “Mittwoch”, “命令”:3 “errorItems”:[] “的errorCode”:0, “天”:“MI “},” 结束 “:” 22:00 “},{” 初学者 “:” 06:00" , “天”:{ “dayLabel”: “Feiertag”, “命令”:8中, “errorItems”:[] “的errorCode”:0, “天”: “FE”}, “结束”: “22:00”},{ “初学者”: “06:00”, “天”:{ “dayLabel”: “蒙塔格” , “命令”:1, “errorItems”:[] “的errorCode”:0, “天”: “MO”}, “结束”: “22:00”},{ “初学者”: “06:00” , “天”:{ “dayLabel”: “Donnerstag”, “命令”:4 “errorItems”:[] “的errorCode”:0, “日”: “DO”} , “结束”: “22:00”},{ “初学者”: “06:00”, “天”:{ “dayLabel”: “Freitag的”, “命令”:5 “errorItems”:[],”的errorCode “:0,” 天 “:” FR “},” 结束 “:” 22:00 “},{” 初学者 “:” 06:00" , “天”:{ “dayLabel”: “Samstag”,”序 “:6中,” errorItems “:[]” 的errorCode “:0,” 天 “:” SA “},” 结束 “:” 22:00 “},{” 初学者 “:” 06:00" ,”天 “:{” dayLabel “:” Dienstag”, “命令”:2 “errorItems”:[] “的errorCode”:0, “天”: “DI”}, “结束”: “22:00”} ],“access”:“”,“url”:“”,“serviceText”:“免费Staubsaugen”,“maestro”:真实,“陪伴”:false,“地址”:“Salzburger Str。 11" , “俱乐部”:假 “的errorCode”:1, “服务”:假, “纬度”: “48.2546591”, “杆”:真, “电话”: “”},{ “kredit”:真, “自我”:虚假,“spritPrice”:[{“金额”:“1.334”,“datAnounce”:“2013-11-27 12:40:18” “errorItems”:[], “错误码” 0 “datValid”:1385552418000, “spritId”: “死”}], “自动售货机”:真正的 “城市”: “布劳瑙”, “开放”:真实, “距离”:2.42, “POSTALCODE”: “5280”, “errorItems”:[], “priceSearchDisabled”:假的, “东经”: “13.048711”, “payMethod”: “Novofleet”, “邮件”:“办公室@ fetrading.at“,”gasStationName“:”FE-Trading GmbH“,”传真“:”4362467223611“,”clubCard“:”“,”openingHours“:[{”beginn“:”06:00“,”day“ :{ “dayLabel”: “蒙塔格”, “命令”:1, “errorItems”:[] “的errorCode”:0, “天”: “MO”}, “结束”: “21:00”},{ “初学者”: “06:00”, “天”:{ “dayLabel”: “Donnerstag”, “命令”:4 “errorItems”:[] “的errorCode”:0, “日”: “DO”} , “结束”: “21:00”},{ “初学者”: “06:00”, “天”:{ “dayLabel”: “Dienstag”, “命令”:2 “errorItems”:[],”的errorCode “:0,” 天 “:” DI “},” 结束 “:” 21:00 “},{” 初学者 “:” 06:00" , “天”:{ “dayLabel”: “Samstag”,”序 “:6中,” errorItems“: [] “的errorCode”:0, “天”: “SA”}, “结束”: “21:00”},{ “初学者”: “08:00”, “天”:{ “dayLabel”:”桑塔格 “ ”命令“:7, ”errorItems“:[] ”的errorCode“:0, ”天“: ”SO“}, ”结束“: ”18:00“},{ ”初学者“:” 08: 00" , “天”:{ “dayLabel”: “Feiertag”, “命令”:8中, “errorItems”:[] “的errorCode”:0, “天”: “FE。”}, “结束”:“18 :00 “},{” 初学者 “:” 06:00" , “天”:{ “dayLabel”: “Freitag的”, “命令”:5 “errorItems”:[] “的errorCode”:0,“天“:” FR “},” 结束 “:” 21:00 “},{” 初学者 “:” 06:00" , “天”:{ “dayLabel”: “Mittwoch”, “命令”:3“,errorItems “:[]” 的errorCode “:0,” 天 “:” MI “},” 结束 “:” 21:00 “}],” 访问 “:””, “URL”: “http://www.fe-trading.at” ,“serviceText”:“”,“maestro”:true,“companionship”:false,“address”:“HoferStraße1(\”Hofer Parkplatz \“)”,“club”:false,“errorCode”:1, “服务”:假, “纬度”: “48.244911”, “杆”:假, “电话”: “4362467223634”}]

6 个答案:

答案 0 :(得分:3)

这样可行。

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();}

答案 1 :(得分:0)

不仅仅是使用String,还可以使用ArrayList或List并添加每个价格。

像这样,

 ArrayList<String> prices=new ArrayList<String>

在循环中写下面的代码。

 String amount = c.getString("amount");
 prices.add(amount);

您可以询问是否有任何进一步的疑问:)

答案 2 :(得分:0)

这是我从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();
}

答案 3 :(得分:0)

试试这个,

// 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();
            }

答案 4 :(得分:0)

这可能对你有帮助......

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
    }
}

只需将String响应传递给loadAmountFrom(String)方法......

答案 5 :(得分:0)

您的数组spritPrice长度为1:

  "spritPrice":[
     {
        "amount":"1.329",
        "datAnounce":"2013-11-27 13:05:07",
        "errorItems":[

        ],
        "errorCode":0,
        "datValid":1385553907000,
        "spritId":"DIE"
     }
  ],

您的根元素必须是JSONArray,而不是JSONObject

一个很好的链接:http://jsonformatter.curiousconcept.com/

它表明您的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"
   }
]