【问题标题】:How to parse from this JSONString?如何从这个 JSONString 解析?
【发布时间】:2013-11-27 12:41:23
【问题描述】:

我通过HTTP Request 得到以下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();
    }

这适用于第一个spritPrice Array。但是我怎样才能得到另一个spritPrice Arrayamounts 呢?

我真的不知道如何实现这一点。谁能帮帮我。

这是我的完整 JSON 字符串的链接:>>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":"Mittoch","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","订单":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":"Mittoch","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,"纬度":"48.244911","bar":false,"电话":"4362467223634"}]

【问题讨论】:

  • 我只能参考你是怎么做的
  • 请。会很棒
  • 您发布了错误的 JSON 响应。更新您的 JSON 字符串
  • 不,这是我真正得到的。它只是一个更长的字符串,具有更多的 spritPrices。但就是这样。

标签: java android json http


【解决方案1】:

这会起作用。

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

【讨论】:

    【解决方案2】:

    试试这个,

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

    【讨论】:

    • 它给了我10倍的第一个价格。
    • 不错。我改变了一些东西。现在它正在工作。我编辑了你的帖子,然后我接受了。
    【解决方案3】:

    不只是取String,还取ArrayList或List并添加每个价格。

    这样,

     ArrayList<String> prices=new ArrayList<String>
    

    在循环中编写以下代码。

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

    如果您有任何进一步的疑问,您可以询问:)

    【讨论】:

    • 它不会改变任何东西。
    • 稍后您可以访问 ArrayList.for 中的所有价格,例如 String price1=prices.get(0);字符串 price2=prices.get(1);
    • 不过还是第一价
    • 稍后您可以访问 ArrayList.for 中的所有价格,例如 String price1=prices.get(0);字符串 price2=prices.get(1);
    • 没有。只有第一个价格。循环结束于第一个spritPrice
    【解决方案4】:

    您的数组 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"
       }
    ]
    

    【讨论】:

      【解决方案5】:

      这可能对你有帮助...

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

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

      【讨论】:

        【解决方案6】:

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

        【讨论】:

          猜你喜欢
          • 2022-01-10
          • 2013-10-08
          • 1970-01-01
          • 2016-02-10
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多