zyandyz003 发表于 2019-5-27 00:12:54

求助,C# 处理json数据报错

求助,C# 处理json数据报错
这是闲的没事想用unity做一个天气预报的APP
在访问这个网址的时候出了一些问题
返回的JSON 数据是这样的

{
"success": "1",
"result": [
    {
      "weaid": "1",
      "days": "2019-05-26",
      "week": "星期日",
      "cityno": "beijing",
      "citynm": "北京",
      "cityid": "101010100",
      "temperature": "16",
      "humidity": "0%/0%",
      "weather": "晴",
      "weather_icon": "http://api.k780.com/upload/weather/d/0.gif",
      "weather_icon1": "http://api.k780.com/upload/weather/n/0.gif",
      "wind": "北风",
      "winp": "4-5级",
      "temp_high": "16",
      "temp_low": "16",
      "humi_high": "0",
      "humi_low": "0",
      "weatid": "1",
      "weatid1": "1",
      "windid": "8",
      "winpid": "2",
      "weather_iconid": "0",
      "weather_iconid1": "0"
    },
    {
      "weaid": "1",
      "days": "2019-05-27",
      "week": "星期一",
      "cityno": "beijing",
      "citynm": "北京",
      "cityid": "101010100",
      "temperature": "29℃/15℃",
      "humidity": "0%/0%",
      "weather": "晴",
      "weather_icon": "http://api.k780.com/upload/weather/d/0.gif",
      "weather_icon1": "http://api.k780.com/upload/weather/n/0.gif",
      "wind": "北风转西风",
      "winp": "3-4级转3-4级",
      "temp_high": "29",
      "temp_low": "15",
      "humi_high": "0",
      "humi_low": "0",
      "weatid": "1",
      "weatid1": "1",
      "windid": "8",
      "winpid": "1",
      "weather_iconid": "0",
      "weather_iconid1": "0"
    },
    {
      "weaid": "1",
      "days": "2019-05-28",
      "week": "星期二",
      "cityno": "beijing",
      "citynm": "北京",
      "cityid": "101010100",
      "temperature": "32℃/17℃",
      "humidity": "0%/0%",
      "weather": "晴",
      "weather_icon": "http://api.k780.com/upload/weather/d/0.gif",
      "weather_icon1": "http://api.k780.com/upload/weather/n/0.gif",
      "wind": "西南风",
      "winp": "3-4级转3-4级",
      "temp_high": "32",
      "temp_low": "17",
      "humi_high": "0",
      "humi_low": "0",
      "weatid": "1",
      "weatid1": "1",
      "windid": "5",
      "winpid": "1",
      "weather_iconid": "0",
      "weather_iconid1": "0"
    },
    {
      "weaid": "1",
      "days": "2019-05-29",
      "week": "星期三",
      "cityno": "beijing",
      "citynm": "北京",
      "cityid": "101010100",
      "temperature": "33℃/21℃",
      "humidity": "0%/0%",
      "weather": "晴转多云",
      "weather_icon": "http://api.k780.com/upload/weather/d/0.gif",
      "weather_icon1": "http://api.k780.com/upload/weather/n/1.gif",
      "wind": "南风转西南风",
      "winp": "小于3级转小于3级",
      "temp_high": "33",
      "temp_low": "21",
      "humi_high": "0",
      "humi_low": "0",
      "weatid": "1",
      "weatid1": "2",
      "windid": "4",
      "winpid": "0",
      "weather_iconid": "0",
      "weather_iconid1": "1"
    },
    {
      "weaid": "1",
      "days": "2019-05-30",
      "week": "星期四",
      "cityno": "beijing",
      "citynm": "北京",
      "cityid": "101010100",
      "temperature": "32℃/17℃",
      "humidity": "0%/0%",
      "weather": "多云转晴",
      "weather_icon": "http://api.k780.com/upload/weather/d/1.gif",
      "weather_icon1": "http://api.k780.com/upload/weather/n/0.gif",
      "wind": "西北风转北风",
      "winp": "4-5级转4-5级",
      "temp_high": "32",
      "temp_low": "17",
      "humi_high": "0",
      "humi_low": "0",
      "weatid": "2",
      "weatid1": "1",
      "windid": "7",
      "winpid": "2",
      "weather_iconid": "1",
      "weather_iconid1": "0"
    },
    {
      "weaid": "1",
      "days": "2019-05-31",
      "week": "星期五",
      "cityno": "beijing",
      "citynm": "北京",
      "cityid": "101010100",
      "temperature": "31℃/16℃",
      "humidity": "0%/0%",
      "weather": "晴",
      "weather_icon": "http://api.k780.com/upload/weather/d/0.gif",
      "weather_icon1": "http://api.k780.com/upload/weather/n/0.gif",
      "wind": "西南风",
      "winp": "小于3级",
      "temp_high": "31",
      "temp_low": "16",
      "humi_high": "0",
      "humi_low": "0",
      "weatid": "1",
      "weatid1": "1",
      "windid": "5",
      "winpid": "0",
      "weather_iconid": "0",
      "weather_iconid1": "0"
    },
    {
      "weaid": "1",
      "days": "2019-06-01",
      "week": "星期六",
      "cityno": "beijing",
      "citynm": "北京",
      "cityid": "101010100",
      "temperature": "34℃/19℃",
      "humidity": "0%/0%",
      "weather": "晴",
      "weather_icon": "http://api.k780.com/upload/weather/d/0.gif",
      "weather_icon1": "http://api.k780.com/upload/weather/n/0.gif",
      "wind": "西南风",
      "winp": "3-4级转3-4级",
      "temp_high": "34",
      "temp_low": "19",
      "humi_high": "0",
      "humi_low": "0",
      "weatid": "1",
      "weatid1": "1",
      "windid": "5",
      "winpid": "1",
      "weather_iconid": "0",
      "weather_iconid1": "0"
    }
]
}


我使用了生成c#实体类
public class Result
{
    /// <summary>
    /// 1
    /// </summary>
    public string weaid { get; set; }
    /// <summary>
    /// 2019-05-26
    /// </summary>
    public DateTime days { get; set; }
    /// <summary>
    /// 星期日
    /// </summary>
    public string week { get; set; }
    /// <summary>
    /// beijing
    /// </summary>
    public string cityno { get; set; }
    /// <summary>
    /// 北京
    /// </summary>
    public string citynm { get; set; }
    /// <summary>
    /// 101010100
    /// </summary>
    public string cityid { get; set; }
    /// <summary>
    /// 16
    /// </summary>
    public string temperature { get; set; }
    /// <summary>
    /// 0%/0%
    /// </summary>
    public string humidity { get; set; }
    /// <summary>
    /// 晴
    /// </summary>
    public string weather { get; set; }
    /// <summary>
    /// http://api.k780.com/upload/weather/d/0.gif
    /// </summary>
    public string weather_icon { get; set; }
    /// <summary>
    /// http://api.k780.com/upload/weather/n/0.gif
    /// </summary>
    public string weather_icon1 { get; set; }
    /// <summary>
    /// 北风
    /// </summary>
    public string wind { get; set; }
    /// <summary>
    /// 4-5级
    /// </summary>
    public string winp { get; set; }
    /// <summary>
    /// 16
    /// </summary>
    public string temp_high { get; set; }
    /// <summary>
    /// 16
    /// </summary>
    public string temp_low { get; set; }
    /// <summary>
    /// 0
    /// </summary>
    public string humi_high { get; set; }
    /// <summary>
    /// 0
    /// </summary>
    public string humi_low { get; set; }
    /// <summary>
    /// 1
    /// </summary>
    public string weatid { get; set; }
    /// <summary>
    /// 1
    /// </summary>
    public string weatid1 { get; set; }
    /// <summary>
    /// 8
    /// </summary>
    public string windid { get; set; }
    /// <summary>
    /// 2
    /// </summary>
    public string winpid { get; set; }
    /// <summary>
    /// 0
    /// </summary>
    public string weather_iconid { get; set; }
    /// <summary>
    /// 0
    /// </summary>
    public string weather_iconid1 { get; set; }
}

public class Root
{
    /// <summary>
    /// 1
    /// </summary>
    public string success { get; set; }
    /// <summary>
    /// Result
    /// </summary>
    public List<Result> result { get; set; }
}



但是在使用以下代码转成 List<>的时候就报错了

var urladdress = "http://api.k780.com/?app=weather.future&weaid=1&appkey=42738&sign=8d01f34cb90f27f9f26a2bc6dce6fae5&format=json";
      Debug.Log("click");
      var data_weather = GetWebClient(urladdress);
      //Debug.Log(data_weather);
      List<Root> jobInfoList = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Root>>(data_weather);


报错在   DeserializeObject   

报错的提示是:
JsonSerializationException: Cannot deserialize JSON object into type 'System.Collections.Generic.List`1'.

不清楚是什么原因啊?
求指导!

是因为我没有对JSON 进行转义吗?


下面附项目源码
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Events;
using System.Net;
using System.IO;
using System.Linq;
using System.Text;
using Newtonsoft.Json.Linq;

public class updateurl : MonoBehaviour
{
    public class ResultItem
    {
      /// <summary>
      ///
      /// </summary>
      public string weaid { get; set; }
      /// <summary>
      /// 日期
      /// </summary>
      public string days { get; set; }
      /// <summary>
      /// 星期几
      /// </summary>
      public string week { get; set; }
      /// <summary>
      /// 城市拼音
      /// </summary>
      public string cityno { get; set; }
      /// <summary>
      /// 城市名称
      /// </summary>
      public string citynm { get; set; }
      /// <summary>
      /// 城市ID
      /// </summary>
      public string cityid { get; set; }
      /// <summary>
      /// 温度
      /// </summary>
      public string temperature { get; set; }
      /// <summary>
      ///
      /// </summary>
      public string humidity { get; set; }
      /// <summary>
      /// 天气情况,晴,多云
      /// </summary>
      public string weather { get; set; }
      /// <summary>
      /// 使用的天气图标
      /// </summary>
      public string weather_icon { get; set; }
      /// <summary>
      /// 使用的第二个天气图标
      /// </summary>
      public string weather_icon1 { get; set; }
      /// <summary>
      /// 风向
      /// </summary>
      public string wind { get; set; }
      /// <summary>
      /// 风力级别
      /// </summary>
      public string winp { get; set; }
      /// <summary>
      /// 最高气温
      /// </summary>
      public string temp_high { get; set; }
      /// <summary>
      /// 最低气温
      /// </summary>
      public string temp_low { get; set; }
      /// <summary>
      ///
      /// </summary>
      public string humi_high { get; set; }
      /// <summary>
      ///
      /// </summary>
      public string humi_low { get; set; }
      /// <summary>
      /// /*天气编号*/
      /// </summary>
      public string weatid { get; set; }
      /// <summary>
      ///
      /// </summary>
      public string weatid1 { get; set; }
      /// <summary>
      /// /*风向编号*/
      /// </summary>
      public string windid { get; set; }
      /// <summary>
      /// /*风力编号*/
      /// </summary>
      public string winpid { get; set; }
      /// <summary>
      ///
      /// </summary>
      public string weather_iconid { get; set; }
      /// <summary>
      /// /*气象图标编号,对应weather_icon 0.gif*/
      /// </summary>
      public string weather_iconid1 { get; set; }
    }

    public class Root
    {
      /// <summary>
      /// 返回成功 是1
      /// </summary>
      public string success { get; set; }
      /// <summary>
      /// 具体信息1
      /// </summary>
      public List<ResultItem> result1 { get; set; }
    }


    // Start is called before the first frame update
    void Start()
    {
      
      GameObject btnObj = GameObject.Find("Canvas/Button_update");
      Button btn = (Button)btnObj.GetComponent<Button>();
      btn.onClick.AddListener(onclick);
    }


    void onclick()
    {
      var urladdress = "http://api.k780.com/?app=weather.future&weaid=1&appkey=42738&sign=8d01f34cb90f27f9f26a2bc6dce6fae5&format=json";
      Debug.Log("click");
      var data_weather = GetWebClient(urladdress);
      //Debug.Log(data_weather);
      List<Root> jobInfoList = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Root>>(data_weather);

      foreach (Root jobInfo in jobInfoList)
      {
            foreach(ResultItem item in jobInfo.result1)
            {
                Debug.Log("城市:" + item.citynm);
                Debug.Log("温度:" + item.temperature);
            }
      }
    }
    public string GetWebClient(string url)
    {
      string strHTML = "";
      WebClient myWebClient = new WebClient();
      Stream myStream = myWebClient.OpenRead(url);
      StreamReader sr = new StreamReader(myStream, System.Text.Encoding.GetEncoding("utf-8"));
      strHTML = sr.ReadToEnd();
      myStream.Close();
      return strHTML;
    }


}



zyandyz003 发表于 2019-5-29 08:11:11

好的,我解决了
因为这个函数是不能嵌套的,
直接单独对resultitem部分进行解析就可以了

C的user 发表于 2019-6-6 18:40:46

首先,你不该去c#交流区吗?

zyandyz003 发表于 2019-6-12 23:17:36

C的user 发表于 2019-6-6 18:40
首先,你不该去c#交流区吗?

好像也没有 C# 的 交流区。Windows程序设计,包括C#吧
页: [1]
查看完整版本: 求助,C# 处理json数据报错