繁体   English   中英

无法在C#中反序列化JSON字符串

[英]Can't deserialize JSON string in C#

我从第三方收到以下JSON字符串:

{
    "Ch": [{
        "i": 100,
        "m": "Time",
        "u": "(sec)",
        "d": 0,
        "z": 0.345313,
        "yi": "0.000000",
        "ya": "6.906250",
        "a": 0,
        "s": 1664,
        "data": "RN]]>"
    }, {
        "i": 101,
        "m": "Stress",
        "u": "(kPa)",
        "d": 0,
        "z": 60,
        "yi": "0.000000",
        "ya": "1200.000000",
        "a": 0
    }, {
        "i": 102,
        "m": "Strain",
        "u": "(micro e)",
        "d": 0,
        "z": 8,
        "yi": "200.000000",
        "ya": "360.000000",
        "a": 0
    }, {
        "i": 103,
        "m": "Stress",
        "u": 360,
        "d": 0,
        "z": 0,
        "yi": "0.000000",
        "ya": "0.000000",
        "a": 0,
        "s": 1664,
        "data": "QVORR`Pb_UQRR</code>OObTNRRUTaWRVRRSaPQPdRRaPNSORRR]]>"
    }, {
        "i": 104,
        "m": "Strain",
        "u": 360,
        "d": 0,
        "z": 0,
        "y": 0,
        "yi": "0.000000",
        "ya": "0.000000",
        "a": 1,
        "s": 1664,
        "data": "SVdRQSP_VWQRQa]]>"
    }]
}

我使用以下类:

public class testCh
{
    public int i { get; set; }
    public string m { get; set; }
    public object u { get; set; }
    public int d { get; set; }
    public double z { get; set; }
    public string yi { get; set; }
    public string ya { get; set; }
    public int a { get; set; }
    [JsonIgnore]
    public int s { get; set; }
    [JsonIgnore]
    public string data { get; set; }

}

public class testRootObject
{
    public List<testCh> tCh { get; set; }
}

最后,我在主班上尝试了这一点:

var response1 = JsonConvert.DeserializeObject<testRootObject>(content1);

foreach (testCh tc in response1.tCh)
{
    string name = tc.m;
}

我确实得到了一个空的testRootObject []

我尝试在testCH类中忽略“ a”和“ data”:

[JsonIgnore]
public int s { get; set; }

[JsonIgnore]
public string data { get; set; }

而且没有用

我不确定为什么我不能反序列化。 这一定是愚蠢的,但是尝试了几天之后,我看不到它。

任何帮助或提示,我们将不胜感激。

我已经检查了并确认。 您在这里遇到的唯一错误是列表对象的名称。 应该是json字符串中的Ch

public class testRootObject
{
    public List<testCh> Ch { get; set; }
}

如果必须将属性名称设置为tCh ,则可以执行以下操作:

public class testRootObject
{
    [JsonProperty("Ch")]
    public List<testCh> tCh { get; set; }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM