简体   繁体   中英

Null class when deserializing Json C#

I have this 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":""}]

Now in c# i tried to deserialize using List<Information> resinfo = JsonConvert.DeserializeObject<List<Information>>(str2);and returns null value

Note that in the pic Null result but the json string is retunrning value as intended Json string

And this is my 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?

Its worth mentioning that I tried this var json = JsonConvert.DeserializeObject<List<Information>>(str2); with the same result

It is because all your properties in your Information class are static . Remove static keyword and try it again.

JsonConvert.DeserializeObject will set only instance properties so they cannot be declared as static.

After execution take a look at removedFines variable (as in your image ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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