Accuweather android json解析错误

时间:2017-11-30 13:04:12

标签: android json api android-studio

这是我第一次在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"
  },
]

很想知道我的错误,现在已经看了一会儿

编辑:

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

我加倍检查它不是拼写错误&amp;输入是一个字符串,而不是int,boolean等。

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

仍然试图弄清楚

1 个答案:

答案 0 :(得分:0)

您试图将JSONArray作为String获取。

这是一段代码。希望它有所帮助...

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