吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 1216|回复: 13
收起左侧

[求助] 【C#】解析json数据遇到点问题

[复制链接]
lizf2019 发表于 2022-4-15 16:23
正在搞一个通过解析json返回的数据显示天气信息的程序,
遇到点问题:
请求的url是这个:https://www.baidu.com/home/other/data/weatherInfo?city=%E6%B4%9B%E9%98%B3

JSON转换C#实体类之后,复制到VS遇到一堆问题:
image.png image.png
好像因为string 不能定义那个数字0、1
我用的是这个dll image.png
求助各位大佬应该怎么办

本帖被以下淘专辑推荐:

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

Taoyi 发表于 2022-4-15 16:39
[JsonProperty(PropertyName ="0")]
public string img0 { get; set; }

试试
zmolli775 发表于 2022-4-15 16:46
本帖最后由 zmolli775 于 2022-4-15 16:50 编辑

[C#] 纯文本查看 复制代码
using System;
using System.Collections.Generic;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace Example
{

    internal partial class Root
    {
        internal class Setting2
        {

            [JsonProperty("city")]
            public string City { get; set; }
        }
    }

    internal partial class Root
    {
        internal class Imgs2
        {

            [JsonProperty("1")]
            public string _1 { get; set; }

            [JsonProperty("0")]
            public string _0 { get; set; }
        }
    }

    internal partial class Root
    {
        internal class Today2
        {

            [JsonProperty("time")]
            public string Time { get; set; }

            [JsonProperty("date")]
            public string Date { get; set; }

            [JsonProperty("img")]
            public string[] Img { get; set; }

            [JsonProperty("condition")]
            public string Condition { get; set; }

            [JsonProperty("wind")]
            public string Wind { get; set; }

            [JsonProperty("temp")]
            public string Temp { get; set; }

            [JsonProperty("link")]
            public string Link { get; set; }

            [JsonProperty("imgs")]
            public Imgs2 Imgs { get; set; }

            [JsonProperty("pm25")]
            public string Pm25 { get; set; }

            [JsonProperty("pollution")]
            public string Pollution { get; set; }

            [JsonProperty("pm25url")]
            public string Pm25url { get; set; }
        }
    }

    internal partial class Root
    {
        internal class Imgs3
        {

            [JsonProperty("1")]
            public string _1 { get; set; }

            [JsonProperty("0")]
            public string _0 { get; set; }
        }
    }

    internal partial class Root
    {
        internal class Tomorrow2
        {

            [JsonProperty("time")]
            public string Time { get; set; }

            [JsonProperty("date")]
            public string Date { get; set; }

            [JsonProperty("img")]
            public string[] Img { get; set; }

            [JsonProperty("condition")]
            public string Condition { get; set; }

            [JsonProperty("wind")]
            public string Wind { get; set; }

            [JsonProperty("temp")]
            public string Temp { get; set; }

            [JsonProperty("link")]
            public string Link { get; set; }

            [JsonProperty("imgs")]
            public Imgs3 Imgs { get; set; }

            [JsonProperty("pm25")]
            public string Pm25 { get; set; }

            [JsonProperty("pollution")]
            public string Pollution { get; set; }

            [JsonProperty("pm25url")]
            public string Pm25url { get; set; }
        }
    }

    internal partial class Root
    {
        internal class Imgs4
        {

            [JsonProperty("1")]
            public string _1 { get; set; }

            [JsonProperty("0")]
            public string _0 { get; set; }
        }
    }

    internal partial class Root
    {
        internal class Thirdday2
        {

            [JsonProperty("time")]
            public string Time { get; set; }

            [JsonProperty("date")]
            public string Date { get; set; }

            [JsonProperty("img")]
            public string[] Img { get; set; }

            [JsonProperty("condition")]
            public string Condition { get; set; }

            [JsonProperty("wind")]
            public string Wind { get; set; }

            [JsonProperty("temp")]
            public string Temp { get; set; }

            [JsonProperty("link")]
            public string Link { get; set; }

            [JsonProperty("imgs")]
            public Imgs4 Imgs { get; set; }

            [JsonProperty("pm25")]
            public string Pm25 { get; set; }

            [JsonProperty("pollution")]
            public string Pollution { get; set; }

            [JsonProperty("pm25url")]
            public string Pm25url { get; set; }
        }
    }

    internal partial class Root
    {
        internal class Imgs5
        {

            [JsonProperty("1")]
            public string _1 { get; set; }

            [JsonProperty("0")]
            public string _0 { get; set; }
        }
    }

    internal partial class Root
    {
        internal class Fourthday2
        {

            [JsonProperty("time")]
            public string Time { get; set; }

            [JsonProperty("date")]
            public string Date { get; set; }

            [JsonProperty("img")]
            public string[] Img { get; set; }

            [JsonProperty("condition")]
            public string Condition { get; set; }

            [JsonProperty("wind")]
            public string Wind { get; set; }

            [JsonProperty("temp")]
            public string Temp { get; set; }

            [JsonProperty("link")]
            public string Link { get; set; }

            [JsonProperty("imgs")]
            public Imgs5 Imgs { get; set; }
        }
    }

    internal partial class Root
    {
        internal class Imgs6
        {

            [JsonProperty("1")]
            public string _1 { get; set; }

            [JsonProperty("0")]
            public string _0 { get; set; }
        }
    }

    internal partial class Root
    {
        internal class Fifthday2
        {

            [JsonProperty("time")]
            public string Time { get; set; }

            [JsonProperty("date")]
            public string Date { get; set; }

            [JsonProperty("img")]
            public string[] Img { get; set; }

            [JsonProperty("condition")]
            public string Condition { get; set; }

            [JsonProperty("wind")]
            public string Wind { get; set; }

            [JsonProperty("temp")]
            public string Temp { get; set; }

            [JsonProperty("link")]
            public string Link { get; set; }

            [JsonProperty("imgs")]
            public Imgs6 Imgs { get; set; }
        }
    }

    internal partial class Root
    {
        internal class Source2
        {

            [JsonProperty("name")]
            public string Name { get; set; }

            [JsonProperty("url")]
            public string Url { get; set; }
        }
    }

    internal partial class Root
    {
        internal class Calendar2
        {

            [JsonProperty("time")]
            public string Time { get; set; }

            [JsonProperty("lunar")]
            public string Lunar { get; set; }

            [JsonProperty("festival")]
            public bool Festival { get; set; }

            [JsonProperty("weatherSourceUrl")]
            public string WeatherSourceUrl { get; set; }
        }
    }

    internal partial class Root
    {
        internal class Content2
        {

            [JsonProperty("week")]
            public string Week { get; set; }

            [JsonProperty("city")]
            public string City { get; set; }

            [JsonProperty("today")]
            public Today2 Today { get; set; }

            [JsonProperty("tomorrow")]
            public Tomorrow2 Tomorrow { get; set; }

            [JsonProperty("thirdday")]
            public Thirdday2 Thirdday { get; set; }

            [JsonProperty("fourthday")]
            public Fourthday2 Fourthday { get; set; }

            [JsonProperty("fifthday")]
            public Fifthday2 Fifthday { get; set; }

            [JsonProperty("linkseven")]
            public string Linkseven { get; set; }

            [JsonProperty("source")]
            public Source2 Source { get; set; }

            [JsonProperty("cityname")]
            public string Cityname { get; set; }

            [JsonProperty("calendar")]
            public Calendar2 Calendar { get; set; }

            [JsonProperty("currenttemp")]
            public string Currenttemp { get; set; }

            [JsonProperty("pslink")]
            public string Pslink { get; set; }

            [JsonProperty("weatherType")]
            public string WeatherType { get; set; }

            [JsonProperty("isauto")]
            public bool Isauto { get; set; }

            [JsonProperty("ipcity")]
            public string Ipcity { get; set; }

            [JsonProperty("province")]
            public string Province { get; set; }
        }
    }

    internal partial class Root
    {
        internal class Weather2
        {

            [JsonProperty("setting")]
            public Setting2 Setting { get; set; }

            [JsonProperty("content")]
            public Content2 Content { get; set; }
        }
    }

    internal partial class Root
    {
        internal class Data2
        {

            [JsonProperty("weather")]
            public Weather2 Weather { get; set; }
        }
    }

    internal partial class Root
    {

        [JsonProperty("errNo")]
        public string ErrNo { get; set; }

        [JsonProperty("data")]
        public Data2 Data { get; set; }
    }

}

免费评分

参与人数 1吾爱币 +1 热心值 +1 收起 理由
lizf2019 + 1 + 1 感谢大佬指点

查看全部评分

jidesheng6 发表于 2022-4-15 16:47
[C#] 纯文本查看 复制代码
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;

JObject Obj = GetResponse("http://xxx.com");
JToken RespCode = Obj["code"];


你可以拿到以后直接解析,就和上面的例子一样
披星代月 发表于 2022-4-15 16:54
了解下Json.net的JsonProperty 标签,你就知道怎么解决了,二楼,三楼都给你解决办法了。
 楼主| lizf2019 发表于 2022-4-15 17:03
zmolli775 发表于 2022-4-15 16:46
[mw_shl_code=csharp,true]
using System;
using System.Collections.Generic;

感谢指点,请问大佬这个是怎么转换出来的呢(也有工具吗)
 楼主| lizf2019 发表于 2022-4-15 17:04
jidesheng6 发表于 2022-4-15 16:47
[mw_shl_code=csharp,true]
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;

不太懂这个用法,“code”  这个是要替换成要解析出来的字符串吗
jidesheng6 发表于 2022-4-15 18:09
lizf2019 发表于 2022-4-15 17:04
不太懂这个用法,“code”  这个是要替换成要解析出来的字符串吗

对,你试试看。就是和字典取值一样

免费评分

参与人数 1吾爱币 +1 收起 理由
lizf2019 + 1 谢谢@Thanks!

查看全部评分

你好港屿 发表于 2022-4-15 18:15
JToken Jobj = JObject.Parse(httpResult) as JToken;
我一般都用户这个区解析一个一个获取下来
 楼主| lizf2019 发表于 2022-4-15 19:47
jidesheng6 发表于 2022-4-15 18:09
对,你试试看。就是和字典取值一样

假如要取出today中的date是直接填“”date“”吗,似乎date有许多个
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-11-25 15:34

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

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