鱼C论坛

 找回密码
 立即注册
查看: 2545|回复: 3

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

[复制链接]
发表于 2019-5-27 00:12:54 | 显示全部楼层 |阅读模式
100鱼币
求助,C# 处理json数据报错
这是闲的没事想用unity做一个天气预报的APP
在访问这个网址的时候出了一些问题
返回的JSON 数据是这样的

  1. {
  2.   "success": "1",
  3.   "result": [
  4.     {
  5.       "weaid": "1",
  6.       "days": "2019-05-26",
  7.       "week": "星期日",
  8.       "cityno": "beijing",
  9.       "citynm": "北京",
  10.       "cityid": "101010100",
  11.       "temperature": "16",
  12.       "humidity": "0%/0%",
  13.       "weather": "晴",
  14.       "weather_icon": "http://api.k780.com/upload/weather/d/0.gif",
  15.       "weather_icon1": "http://api.k780.com/upload/weather/n/0.gif",
  16.       "wind": "北风",
  17.       "winp": "4-5级",
  18.       "temp_high": "16",
  19.       "temp_low": "16",
  20.       "humi_high": "0",
  21.       "humi_low": "0",
  22.       "weatid": "1",
  23.       "weatid1": "1",
  24.       "windid": "8",
  25.       "winpid": "2",
  26.       "weather_iconid": "0",
  27.       "weather_iconid1": "0"
  28.     },
  29.     {
  30.       "weaid": "1",
  31.       "days": "2019-05-27",
  32.       "week": "星期一",
  33.       "cityno": "beijing",
  34.       "citynm": "北京",
  35.       "cityid": "101010100",
  36.       "temperature": "29℃/15℃",
  37.       "humidity": "0%/0%",
  38.       "weather": "晴",
  39.       "weather_icon": "http://api.k780.com/upload/weather/d/0.gif",
  40.       "weather_icon1": "http://api.k780.com/upload/weather/n/0.gif",
  41.       "wind": "北风转西风",
  42.       "winp": "3-4级转3-4级",
  43.       "temp_high": "29",
  44.       "temp_low": "15",
  45.       "humi_high": "0",
  46.       "humi_low": "0",
  47.       "weatid": "1",
  48.       "weatid1": "1",
  49.       "windid": "8",
  50.       "winpid": "1",
  51.       "weather_iconid": "0",
  52.       "weather_iconid1": "0"
  53.     },
  54.     {
  55.       "weaid": "1",
  56.       "days": "2019-05-28",
  57.       "week": "星期二",
  58.       "cityno": "beijing",
  59.       "citynm": "北京",
  60.       "cityid": "101010100",
  61.       "temperature": "32℃/17℃",
  62.       "humidity": "0%/0%",
  63.       "weather": "晴",
  64.       "weather_icon": "http://api.k780.com/upload/weather/d/0.gif",
  65.       "weather_icon1": "http://api.k780.com/upload/weather/n/0.gif",
  66.       "wind": "西南风",
  67.       "winp": "3-4级转3-4级",
  68.       "temp_high": "32",
  69.       "temp_low": "17",
  70.       "humi_high": "0",
  71.       "humi_low": "0",
  72.       "weatid": "1",
  73.       "weatid1": "1",
  74.       "windid": "5",
  75.       "winpid": "1",
  76.       "weather_iconid": "0",
  77.       "weather_iconid1": "0"
  78.     },
  79.     {
  80.       "weaid": "1",
  81.       "days": "2019-05-29",
  82.       "week": "星期三",
  83.       "cityno": "beijing",
  84.       "citynm": "北京",
  85.       "cityid": "101010100",
  86.       "temperature": "33℃/21℃",
  87.       "humidity": "0%/0%",
  88.       "weather": "晴转多云",
  89.       "weather_icon": "http://api.k780.com/upload/weather/d/0.gif",
  90.       "weather_icon1": "http://api.k780.com/upload/weather/n/1.gif",
  91.       "wind": "南风转西南风",
  92.       "winp": "小于3级转小于3级",
  93.       "temp_high": "33",
  94.       "temp_low": "21",
  95.       "humi_high": "0",
  96.       "humi_low": "0",
  97.       "weatid": "1",
  98.       "weatid1": "2",
  99.       "windid": "4",
  100.       "winpid": "0",
  101.       "weather_iconid": "0",
  102.       "weather_iconid1": "1"
  103.     },
  104.     {
  105.       "weaid": "1",
  106.       "days": "2019-05-30",
  107.       "week": "星期四",
  108.       "cityno": "beijing",
  109.       "citynm": "北京",
  110.       "cityid": "101010100",
  111.       "temperature": "32℃/17℃",
  112.       "humidity": "0%/0%",
  113.       "weather": "多云转晴",
  114.       "weather_icon": "http://api.k780.com/upload/weather/d/1.gif",
  115.       "weather_icon1": "http://api.k780.com/upload/weather/n/0.gif",
  116.       "wind": "西北风转北风",
  117.       "winp": "4-5级转4-5级",
  118.       "temp_high": "32",
  119.       "temp_low": "17",
  120.       "humi_high": "0",
  121.       "humi_low": "0",
  122.       "weatid": "2",
  123.       "weatid1": "1",
  124.       "windid": "7",
  125.       "winpid": "2",
  126.       "weather_iconid": "1",
  127.       "weather_iconid1": "0"
  128.     },
  129.     {
  130.       "weaid": "1",
  131.       "days": "2019-05-31",
  132.       "week": "星期五",
  133.       "cityno": "beijing",
  134.       "citynm": "北京",
  135.       "cityid": "101010100",
  136.       "temperature": "31℃/16℃",
  137.       "humidity": "0%/0%",
  138.       "weather": "晴",
  139.       "weather_icon": "http://api.k780.com/upload/weather/d/0.gif",
  140.       "weather_icon1": "http://api.k780.com/upload/weather/n/0.gif",
  141.       "wind": "西南风",
  142.       "winp": "小于3级",
  143.       "temp_high": "31",
  144.       "temp_low": "16",
  145.       "humi_high": "0",
  146.       "humi_low": "0",
  147.       "weatid": "1",
  148.       "weatid1": "1",
  149.       "windid": "5",
  150.       "winpid": "0",
  151.       "weather_iconid": "0",
  152.       "weather_iconid1": "0"
  153.     },
  154.     {
  155.       "weaid": "1",
  156.       "days": "2019-06-01",
  157.       "week": "星期六",
  158.       "cityno": "beijing",
  159.       "citynm": "北京",
  160.       "cityid": "101010100",
  161.       "temperature": "34℃/19℃",
  162.       "humidity": "0%/0%",
  163.       "weather": "晴",
  164.       "weather_icon": "http://api.k780.com/upload/weather/d/0.gif",
  165.       "weather_icon1": "http://api.k780.com/upload/weather/n/0.gif",
  166.       "wind": "西南风",
  167.       "winp": "3-4级转3-4级",
  168.       "temp_high": "34",
  169.       "temp_low": "19",
  170.       "humi_high": "0",
  171.       "humi_low": "0",
  172.       "weatid": "1",
  173.       "weatid1": "1",
  174.       "windid": "5",
  175.       "winpid": "1",
  176.       "weather_iconid": "0",
  177.       "weather_iconid1": "0"
  178.     }
  179.   ]
  180. }
复制代码



我使用了生成c#实体类
  1. public class Result
  2. {
  3.     /// <summary>
  4.     /// 1
  5.     /// </summary>
  6.     public string weaid { get; set; }
  7.     /// <summary>
  8.     /// 2019-05-26
  9.     /// </summary>
  10.     public DateTime days { get; set; }
  11.     /// <summary>
  12.     /// 星期日
  13.     /// </summary>
  14.     public string week { get; set; }
  15.     /// <summary>
  16.     /// beijing
  17.     /// </summary>
  18.     public string cityno { get; set; }
  19.     /// <summary>
  20.     /// 北京
  21.     /// </summary>
  22.     public string citynm { get; set; }
  23.     /// <summary>
  24.     /// 101010100
  25.     /// </summary>
  26.     public string cityid { get; set; }
  27.     /// <summary>
  28.     /// 16
  29.     /// </summary>
  30.     public string temperature { get; set; }
  31.     /// <summary>
  32.     /// 0%/0%
  33.     /// </summary>
  34.     public string humidity { get; set; }
  35.     /// <summary>
  36.     /// 晴
  37.     /// </summary>
  38.     public string weather { get; set; }
  39.     /// <summary>
  40.     /// http://api.k780.com/upload/weather/d/0.gif
  41.     /// </summary>
  42.     public string weather_icon { get; set; }
  43.     /// <summary>
  44.     /// http://api.k780.com/upload/weather/n/0.gif
  45.     /// </summary>
  46.     public string weather_icon1 { get; set; }
  47.     /// <summary>
  48.     /// 北风
  49.     /// </summary>
  50.     public string wind { get; set; }
  51.     /// <summary>
  52.     /// 4-5级
  53.     /// </summary>
  54.     public string winp { get; set; }
  55.     /// <summary>
  56.     /// 16
  57.     /// </summary>
  58.     public string temp_high { get; set; }
  59.     /// <summary>
  60.     /// 16
  61.     /// </summary>
  62.     public string temp_low { get; set; }
  63.     /// <summary>
  64.     /// 0
  65.     /// </summary>
  66.     public string humi_high { get; set; }
  67.     /// <summary>
  68.     /// 0
  69.     /// </summary>
  70.     public string humi_low { get; set; }
  71.     /// <summary>
  72.     /// 1
  73.     /// </summary>
  74.     public string weatid { get; set; }
  75.     /// <summary>
  76.     /// 1
  77.     /// </summary>
  78.     public string weatid1 { get; set; }
  79.     /// <summary>
  80.     /// 8
  81.     /// </summary>
  82.     public string windid { get; set; }
  83.     /// <summary>
  84.     /// 2
  85.     /// </summary>
  86.     public string winpid { get; set; }
  87.     /// <summary>
  88.     /// 0
  89.     /// </summary>
  90.     public string weather_iconid { get; set; }
  91.     /// <summary>
  92.     /// 0
  93.     /// </summary>
  94.     public string weather_iconid1 { get; set; }
  95. }

  96. public class Root
  97. {
  98.     /// <summary>
  99.     /// 1
  100.     /// </summary>
  101.     public string success { get; set; }
  102.     /// <summary>
  103.     /// Result
  104.     /// </summary>
  105.     public List<Result> result { get; set; }
  106. }
复制代码



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

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



报错在   DeserializeObject   

报错的提示是:
  1. JsonSerializationException: Cannot deserialize JSON object into type 'System.Collections.Generic.List`1[updateurl+Root]'.
复制代码


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

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


下面附项目源码
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using UnityEngine.Events;
  4. using System.Net;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Text;
  8. using Newtonsoft.Json.Linq;

  9. public class updateurl : MonoBehaviour
  10. {
  11.     public class ResultItem
  12.     {
  13.         /// <summary>
  14.         ///
  15.         /// </summary>
  16.         public string weaid { get; set; }
  17.         /// <summary>
  18.         /// 日期
  19.         /// </summary>
  20.         public string days { get; set; }
  21.         /// <summary>
  22.         /// 星期几
  23.         /// </summary>
  24.         public string week { get; set; }
  25.         /// <summary>
  26.         /// 城市拼音
  27.         /// </summary>
  28.         public string cityno { get; set; }
  29.         /// <summary>
  30.         /// 城市名称
  31.         /// </summary>
  32.         public string citynm { get; set; }
  33.         /// <summary>
  34.         /// 城市ID
  35.         /// </summary>
  36.         public string cityid { get; set; }
  37.         /// <summary>
  38.         /// 温度
  39.         /// </summary>
  40.         public string temperature { get; set; }
  41.         /// <summary>
  42.         ///
  43.         /// </summary>
  44.         public string humidity { get; set; }
  45.         /// <summary>
  46.         /// 天气情况,晴,多云
  47.         /// </summary>
  48.         public string weather { get; set; }
  49.         /// <summary>
  50.         /// 使用的天气图标
  51.         /// </summary>
  52.         public string weather_icon { get; set; }
  53.         /// <summary>
  54.         /// 使用的第二个天气图标
  55.         /// </summary>
  56.         public string weather_icon1 { get; set; }
  57.         /// <summary>
  58.         /// 风向
  59.         /// </summary>
  60.         public string wind { get; set; }
  61.         /// <summary>
  62.         /// 风力级别
  63.         /// </summary>
  64.         public string winp { get; set; }
  65.         /// <summary>
  66.         /// 最高气温
  67.         /// </summary>
  68.         public string temp_high { get; set; }
  69.         /// <summary>
  70.         /// 最低气温
  71.         /// </summary>
  72.         public string temp_low { get; set; }
  73.         /// <summary>
  74.         ///
  75.         /// </summary>
  76.         public string humi_high { get; set; }
  77.         /// <summary>
  78.         ///
  79.         /// </summary>
  80.         public string humi_low { get; set; }
  81.         /// <summary>
  82.         /// /*天气编号*/
  83.         /// </summary>
  84.         public string weatid { get; set; }
  85.         /// <summary>
  86.         ///
  87.         /// </summary>
  88.         public string weatid1 { get; set; }
  89.         /// <summary>
  90.         /// /*风向编号*/
  91.         /// </summary>
  92.         public string windid { get; set; }
  93.         /// <summary>
  94.         /// /*风力编号*/
  95.         /// </summary>
  96.         public string winpid { get; set; }
  97.         /// <summary>
  98.         ///
  99.         /// </summary>
  100.         public string weather_iconid { get; set; }
  101.         /// <summary>
  102.         /// /*气象图标编号,对应weather_icon 0.gif*/
  103.         /// </summary>
  104.         public string weather_iconid1 { get; set; }
  105.     }

  106.     public class Root
  107.     {
  108.         /// <summary>
  109.         /// 返回成功 是1
  110.         /// </summary>
  111.         public string success { get; set; }
  112.         /// <summary>
  113.         /// 具体信息1
  114.         /// </summary>
  115.         public List<ResultItem> result1 { get; set; }
  116.     }


  117.     // Start is called before the first frame update
  118.     void Start()
  119.     {
  120.         
  121.         GameObject btnObj = GameObject.Find("Canvas/Button_update");
  122.         Button btn = (Button)btnObj.GetComponent<Button>();
  123.         btn.onClick.AddListener(onclick);
  124.     }


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

  132.         foreach (Root jobInfo in jobInfoList)
  133.         {
  134.             foreach(ResultItem item in jobInfo.result1)
  135.             {
  136.                 Debug.Log("城市:" + item.citynm);
  137.                 Debug.Log("温度:" + item.temperature);
  138.             }
  139.         }
  140.     }
  141.     public string GetWebClient(string url)
  142.     {
  143.         string strHTML = "";
  144.         WebClient myWebClient = new WebClient();
  145.         Stream myStream = myWebClient.OpenRead(url);
  146.         StreamReader sr = new StreamReader(myStream, System.Text.Encoding.GetEncoding("utf-8"));
  147.         strHTML = sr.ReadToEnd();
  148.         myStream.Close();
  149.         return strHTML;
  150.     }


  151. }


复制代码

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2019-5-29 08:11:11 | 显示全部楼层
好的,我解决了
因为这个函数是不能嵌套的,
直接单独对  resultitem  部分进行解析就可以了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2019-6-6 18:40:46 From FishC Mobile | 显示全部楼层
首先,你不该去c#交流区吗?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2019-6-12 23:17:36 | 显示全部楼层
C的user 发表于 2019-6-6 18:40
首先,你不该去c#交流区吗?

好像也没有 C# 的 交流区。Windows程序设计,包括C#吧
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-27 12:02

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表