【问题标题】:Accuweather android json parsing errorAccuweather android json解析错误
【发布时间】:2018-05-14 10:14:03
【问题描述】:

这是我第一次在android中使用json,json正在下载中, 我只是得到一个解析错误

API 链接到:

http://dataservice.accuweather.com/forecasts/v1/hourly/12hour/83342?apikey=HDJ7qKj9ZH0XEW41vgj507CVox7GrRE8&language=en-us&details=true&metric=true

我在下面添加了一个更好看的 JSON 示例

这是我的解析代码:

protected JSONObject doInBackground(Pair<String, String>[] pairs) {

    HttpHandler sh = new HttpHandler();
    // Making a request to url and getting response
    String url = "http://dataservice.accuweather.com/forecasts/v1/hourly/12hour/83342?apikey=HDJ7qKj9ZH0XEW41vgj507CVox7GrRE8&language=en-us&details=true&metric=true";
    String jsonStr = sh.makeServiceCall(url);
    System.out.println("Response from url: " + jsonStr);

    if (jsonStr != null) {
        try {

            JSONObject jsonObj = new JSONObject(jsonStr);
            // Getting JSON Array node

            JSONArray getAccuweather = jsonObj.getJSONArray("weather");

            // looping through All Contacts
            for (int i = 0; i < getAccuweather.length(); i++) {
                JSONObject getInfo = getAccuweather.getJSONObject(i);
                String DateTimeArray = getInfo.getString("DateTime");

                String EpochDateTimeArray = getInfo.getString("EpochDateTime");
                String WeatherIconArray = getInfo.getString("WeatherIcon");
                String IconPhraseArray = getInfo.getString("IconPhrase");
                String IsDaylighteArray = getInfo.getString("IsDaylight");
                String TemperatureeArray = getInfo.getString("Temperature");


                String RealFeelTemperatureArray = getInfo.getString("RealFeelTemperature");
                String WetBulbTemperatureArray = getInfo.getString("WetBulbTemperature");
                String DewPointArray = getInfo.getString("DewPoint");
                String WindArray = getInfo.getString("Wind");
                String DirectionArray = getInfo.getString("Direction");
                String WindGustArray = getInfo.getString("WindGust");
                String RelativeHumidityArray = getInfo.getString("RelativeHumidity");
                String CeilingArray = getInfo.getString("Ceiling");
                String UVIndexArray = getInfo.getString("UVIndex");
                String UVIndexTextArray = getInfo.getString("UVIndexText");
                String PrecipitationProbabilityArray = getInfo.getString("PrecipitationProbability");
                String RainProbabilityArray = getInfo.getString("RainProbability");
                String SnowProbabilityArray = getInfo.getString("SnowProbability");
                String IceProbabilityArray = getInfo.getString("IceProbability");
                String TotalLiquidArray = getInfo.getString("TotalLiquid");
                String RainArray = getInfo.getString("Rain");
                String SnowArray = getInfo.getString("Snow");
                String IceArray = getInfo.getString("Ice");
                String CloudCoverArray = getInfo.getString("CloudCover");
                String MobileLinkArray = getInfo.getString("MobileLink");
                String Link = getInfo.getString("Link");

                //  JSON Object (Date & Time Object)
                JSONObject DateTimeObject = getInfo.getJSONObject("DateTime");
                String DateTime = DateTimeObject.getString("DateTime");

                //  JSON Object (EpochDateTimeO bject)
                JSONObject EpochDateObject = getInfo.getJSONObject("EpochDateTime");
                int EpochDate = EpochDateObject.getInt("EpochDateTime");

                //  JSON Object (WeatherIcon Object)
                JSONObject WeatherIconObject = getInfo.getJSONObject("WeatherIcon");
                int WeatherIcon = WeatherIconObject.getInt("WeatherIcon");

                //  JSON Object (IconPhrase Object)
                JSONObject IconPhraseObject = getInfo.getJSONObject("IconPhrase");
                String IconPhrase = IconPhraseObject.getString("IconPhrase");

                //  JSON Object (IsDaylight Object)
                JSONObject IsDaylightObject = getInfo.getJSONObject("IsDaylight");
                boolean IsDaylight = IsDaylightObject.getBoolean("IsDaylight");


                //  JSON Object (Temperature Object)
                JSONObject DateTimeObject_Temperature = DateTimeObject.getJSONObject("Temperature");
                int DateTimeValue = DateTimeObject_Temperature.getInt("Value");
                String Unit = DateTimeObject_Temperature.getString("Unit");
                int UnitType = DateTimeObject_Temperature.getInt("UnitType");

                // JSON Object (RealFeelTemperature Object)
                JSONObject RealFeelTemperatureObject = getInfo.getJSONObject("RealFeelTemperature");
                int RealFeelTemperatureValue = RealFeelTemperatureObject.getInt("Value");
                String RealFeelTemperatureUnit = RealFeelTemperatureObject.getString("Unit");
                int RealFeelTemperatureUnitType = RealFeelTemperatureObject.getInt("UnitType");

                // JSON Object (WetBulbTemperature Object)
                JSONObject WetBulbTemperatureObject = getInfo.getJSONObject("WetBulbTemperature");
                int WetBulbTemperatureValue = WetBulbTemperatureObject.getInt("Value");
                String WetBulbTemperatureeUnit = WetBulbTemperatureObject.getString("Unit");
                int WetBulbTemperatureType = WetBulbTemperatureObject.getInt("UnitType");

                // JSON Object (DewPoint Object)
                JSONObject DewPointObject = getInfo.getJSONObject("DewPoint");
                int DewPointValue = DewPointObject.getInt("Value");
                String DewPointUnit = DewPointObject.getString("Unit");
                int DewPointUnitType = DewPointObject.getInt("UnitType");

                // JSON Object (Wind Object)
                JSONObject WindObject = getInfo.getJSONObject("Wind");
                JSONObject WindObject_Wind = WindObject.getJSONObject("Speed");
                int WindValue = WindObject_Wind.getInt("Value");
                String WindUnit = WindObject_Wind.getString("Unit");
                int WindUnitType = WindObject_Wind.getInt("UnitType");

                JSONObject WindObject_Direction = getInfo.getJSONObject("Direction");
                int Wind_Direction_Degrees = WindObject_Direction.getInt("Degrees");
                String Wind_Direction_Localized = WindObject_Direction.getString("Localized");
                String Wind_Direction_English = WindObject_Direction.getString("English");

                // JSON Object (WindGust Object)
                JSONObject WindGustObject = getInfo.getJSONObject("WindGust");
                JSONObject WindGustObject_WindGuest = WindGustObject.getJSONObject("Speed");
                int WindGuestValue = WindGustObject_WindGuest.getInt("Value");
                String WindGuestUnit = WindGustObject_WindGuest.getString("Unit");
                int WindGuestUnitType = WindGustObject_WindGuest.getInt("UnitType");


                // JSON Object (RelativeHumidity Object)
                JSONObject RelativeHumidityObject = getInfo.getJSONObject("RelativeHumidity");
                int RelativeHumidityUnit = RelativeHumidityObject.getInt("RelativeHumidity");

                JSONObject VisibilityObject = RelativeHumidityObject.getJSONObject("Visibility");
                int Visibiliyt_value = VisibilityObject.getInt("Value");
                String Visibility_Unit = VisibilityObject.getString("Unit");
                int Visibility_UnitType = VisibilityObject.getInt("UnitType");


                // JSON Object (Ceiling Object)
                JSONObject CeilingObject = getInfo.getJSONObject("Visibility");
                int Ceiling_value = CeilingObject.getInt("Value");
                String Ceiling_Unit = CeilingObject.getString("Unit");
                int Ceiling_UnitType = CeilingObject.getInt("UnitType");

                // JSON Object (UVIndex Object)
                JSONObject UVIndexObject = getInfo.getJSONObject("UVIndex");
                String UVIndex = UVIndexObject.getString("UVIndex");

                // JSON Object (PrecipitationProbability Object)
                JSONObject PrecipitationProbabilityObject = getInfo.getJSONObject("PrecipitationProbability");
                int PrecipitationProbability = PrecipitationProbabilityObject.getInt("PrecipitationProbability");

                // JSON Object (UVIndexText Object)
                JSONObject UVIndexTextObject = getInfo.getJSONObject("UVIndexText");
                int UVIndexText = UVIndexTextObject.getInt("UVIndexText");

                // JSON Object (SnowProbability Object)
                JSONObject SnowProbabilityObject = getInfo.getJSONObject("SnowProbability");
                int SnowProbability = SnowProbabilityObject.getInt("SnowProbability");

                // JSON Object (IceProbability Object)
                JSONObject IceProbabilityObject = getInfo.getJSONObject("IceProbability");
                int IceProbability = IceProbabilityObject.getInt("IceProbability");


                // JSON Object (TotalLiquid Object)
                JSONObject TotalLiquidObject = getInfo.getJSONObject("TotalLiquid");
                int TotalLiquidValue = TotalLiquidObject.getInt("Value");
                String TotalLiquidUnit = TotalLiquidObject.getString("Unit");
                int TotalLiquidUnitType = TotalLiquidObject.getInt("UnitType");

                // JSON Object (Rain Object)
                JSONObject RainObject = getInfo.getJSONObject("Rain");
                int RainValue = RainObject.getInt("Value");
                String RainUnit = RainObject.getString("Unit");
                int RainUnitType = RainObject.getInt("UnitType");

                // JSON Object (Snow Object)
                JSONObject SnowObject = getInfo.getJSONObject("Snow");
                int SnowValue = SnowObject.getInt("Value");
                String SnowUnit = SnowObject.getString("Unit");
                int SnowUnitType = SnowObject.getInt("UnitType");

                // JSON Object (Ice Object)
                JSONObject IceObject = getInfo.getJSONObject("Ice");
                int IceValue = IceObject.getInt("Value");
                String IceUnit = IceObject.getString("Unit");
                int IceUnitType = IceObject.getInt("UnitType");

                // JSON Object (MobileLink Object)
                JSONObject MobileLinkObject = getInfo.getJSONObject("MobileLink");
                String MobileLink = MobileLinkObject.getString("MobileLink");

                // JSON Object (Link Object)
                JSONObject LinkObject = getInfo.getJSONObject("Link");
                String Link_value = LinkObject.getString("Link");

                System.out.println("Some of the outputs : " + Visibiliyt_value +"\n" + Visibility_Unit +"\n" + DateTimeValue ) ;

            }

            }catch (final JSONException e) {
            Log.e(TAG, "Json parsing error: " + e.getMessage());

            new Thread(new Runnable() {
                public void run() {

                    System.out.println("Json parsing error: " + e.getMessage());
                }
            }).start();
        }
        }else {
        System.out.print("Couldn't get json from server.");

        new Thread(new Runnable() {
            public void run() {

                System.out.println("Couldn't get json from server. Check LogCat for possible errors!");
            }
        }).start();
    }


        return null;

}

JSON 示例:

[
  {
    "DateTime": "2017-11-29T21:00:00+08:00",
    "EpochDateTime": 1511960400,
    "WeatherIcon": 35,
    "IconPhrase": "Partly cloudy",
    "IsDaylight": false,
    "Temperature": {
      "Value": -5.8,
      "Unit": "C",
      "UnitType": 17
    },
    "RealFeelTemperature": {
      "Value": -6.7,
      "Unit": "C",
      "UnitType": 17
    },
    "WetBulbTemperature": {
      "Value": -7.4,
      "Unit": "C",
      "UnitType": 17
    },
    "DewPoint": {
      "Value": -12,
      "Unit": "C",
      "UnitType": 17
    },
    "Wind": {
      "Speed": {
        "Value": 7.4,
        "Unit": "km/h",
        "UnitType": 7
      },
      "Direction": {
        "Degrees": 353,
        "Localized": "N",
        "English": "N"
      }
    },
    "WindGust": {
      "Speed": {
        "Value": 11.1,
        "Unit": "km/h",
        "UnitType": 7
      }
    },
    "RelativeHumidity": 62,
    "Visibility": {
      "Value": 16.1,
      "Unit": "km",
      "UnitType": 6
    },
    "Ceiling": {
      "Value": 9144,
      "Unit": "m",
      "UnitType": 5
    },
    "UVIndex": 0,
    "UVIndexText": "Low",
    "PrecipitationProbability": 0,
    "RainProbability": 0,
    "SnowProbability": 0,
    "IceProbability": 0,
    "TotalLiquid": {
      "Value": 0,
      "Unit": "mm",
      "UnitType": 3
    },
    "Rain": {
      "Value": 0,
      "Unit": "mm",
      "UnitType": 3
    },
    "Snow": {
      "Value": 0,
      "Unit": "cm",
      "UnitType": 4
    },
    "Ice": {
      "Value": 0,
      "Unit": "mm",
      "UnitType": 3
    },
    "CloudCover": 30,
    "MobileLink": "http://m.accuweather.com/en/cn/matouying-town/83987/hourly-weather-forecast/83987?day=1&unit=c&lang=en-us",
    "Link": "http://www.accuweather.com/en/cn/matouying-town/83987/hourly-weather-forecast/83987?day=1&hbhhour=21&unit=c&lang=en-us"
  },
]

很想知道我的错误是什么,已经看了一段时间了

编辑:

我在其中一个对象(IconPhrase,第 111 行)上发现了 typeMismatch,

我仔细检查了这不是拼写错误,并且输入是字符串,而不是 int、boolean 等。

我还仔细检查了对象的顺序,一切看起来都井然有序。

还在想办法

【问题讨论】:

  • 1. “我只是得到一个解析错误”,你能更准确一点吗?如果它是由异常触发的,那么您至少应该能够获得该行。我认为没有人想检查您的代码是否有错别字。 e.printStackTrace() 可能有助于加快速度。 2. 您的 JSON 示例是一个 JSON 数组,您的解析代码需要一个带有 weather 属性的 JSON 对象是一个 JSON 数组。现在哪个是正确的?
  • 然后我在其中一个对象上发现了 typeMismatch(IconPhrase,第 111 行),我仔细检查了这不是错字,并且输入是字符串,而不是 int、布尔值等.我还仔细检查了对象的顺序,一切看起来都井井有条。还在想办法
  • 您对IconPhrase 进行了多项检查:第一个getInfo.getString("IconPhrase"),第二个JSONObject IconPhraseObject = getInfo.getJSONObject("IconPhrase");,第三个String IconPhrase = IconPhraseObject.getString("IconPhrase");。这些是矛盾的(首先假设一个字符串,第二个假设一个对象)并且会失败。据我所知,这不是唯一的检查:DateTimeEpochDateTimeWeatherIconIsDaylight ,也许其他人也有相互矛盾的检查。示例:getInfo.getJSONObject("IconPhrase").getString("IconPhrase") 解析 "IconPhrase": { "IconPhrase": "" },所以再次检查。

标签: android json api android-studio


【解决方案1】:

您试图以String 的形式获取JSONArray

这是一个sn-p的代码。希望对你有帮助...

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.jsoup.Jsoup;

import java.io.IOException;

public class Jsonparsing {
    public static void main(String[] args) {
        String json = "";
        try {
            json = Jsoup.connect("http://dataservice.accuweather.com/forecasts/v1/hourly/12hour/83342?apikey=HDJ7qKj9ZH0XEW41vgj507CVox7GrRE8&language=en-us&details=true&metric=true").ignoreContentType(true).execute().body();
            System.out.print(json);
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            JSONArray baseArray = new JSONArray(json);
            for (int i = 0; i < baseArray.length(); i++) {
                JSONObject jsonObject = baseArray.getJSONObject(i);
                String DateTime = jsonObject.getString("DateTime");
                String EpochDateTime = jsonObject.getString("EpochDateTime");
                String WeatherIcon = jsonObject.getString("WeatherIcon");
                String IconPhrase = jsonObject.getString("IconPhrase");
                Boolean IsDaylight = jsonObject.getBoolean("IsDaylight");
                //This is for Temperature
                //Similar thing can be done for RealFeelTemperature, WetBulbTemperature etc 
                JSONObject temperature = jsonObject.getJSONObject("Temperature");
                String temperatureValue = temperature.getString("Value");
                String temperatureUnit = temperature.getString("Unit");
                String temperatureUnitType = temperature.getString("UnitType");

                //Similarly other values can be parsed
                System.out.println(DateTime);
                System.out.println(EpochDateTime);
                System.out.println(WeatherIcon);
                System.out.println(IconPhrase);
                System.out.println(IsDaylight);
                System.out.println(temperatureValue);
                System.out.println(temperatureUnit);
                System.out.println(temperatureUnitType);

            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多