简体   繁体   English

Null class 反序列化时 Json C#

[英]Null class when deserializing Json C#

I have this PHP我有这个 PHP

            if ($result->num_rows === 1) {
                    $sql = ("SELECT username, email, plan, activationdate, terminationdate FROM users WHERE username = '$username' LIMIT 1");
$res = mysqli_query($conn,$sql);
if ($res->num_rows === 1) {
while($row = mysqli_fetch_object($res)){
$arr = array( $row);
echo json_encode($arr);

which returns this json correctly [{"username":"xxxxx","email":"xxxxxx","plan":"0","activationdate":"","terminationdate":""}]正确返回此 json [{"username":"xxxxx","email":"xxxxxx","plan":"0","activationdate":"","terminationdate":""}]

Now in c# i tried to deserialize using List<Information> resinfo = JsonConvert.DeserializeObject<List<Information>>(str2);现在在 c# 我尝试使用List<Information> resinfo = JsonConvert.DeserializeObject<List<Information>>(str2);反序列化and returns null value并返回 null 值

Note that in the pic Null result but the json string is retunrning value as intended Json string请注意,在图片Null 结果中,但 json 字符串按预期返回值Json 字符串

And this is my class这是我的 class

public class Information
{

    public static string username { get; set; }
    public static string email { get; set; }
    public static string plan { get; set; }
    public static string activationdate { get; set; }
    public static string terminationdate { get; set; }
}

Why am I getting null when I try to deserialize into the class and how can I deserialize correctly?当我尝试反序列化到 class 时,为什么会得到 null 以及如何正确反序列化?

Its worth mentioning that I tried this var json = JsonConvert.DeserializeObject<List<Information>>(str2);值得一提的是,我试过这个var json = JsonConvert.DeserializeObject<List<Information>>(str2); with the same result结果相同

It is because all your properties in your Information class are static .这是因为您Information class 中的所有属性都是static Remove static keyword and try it again.删除 static 关键字并重试。

JsonConvert.DeserializeObject will set only instance properties so they cannot be declared as static. JsonConvert.DeserializeObject将仅设置实例属性,因此它们不能声明为 static。

After execution take a look at removedFines variable (as in your image ).执行后查看removedFines变量(如您的图像中所示)。

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

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