0

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

0

1 Answer 1

3

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).

Sign up to request clarification or add additional context in comments.

6 Comments

Thank you for your answer. Removing static gives the following !Image. (I changed the variable to make it more readable)
It is null because it is not executed. Try to put breakpoint a one line under and then take a look at result. If you dont have any code under, just put there var test = 1; for example.
Thank you. It does now return the correct values. One last question. Say that I want to use the string in "username" and display it in a label in another form, or user control in this case. How should I do that. Because simply calling label1.text = Information.username from the user control wont work. Thats why I had the properties static in the first place.
In your case it is a List of Information. You have to call UiInfo[0].username that means you will show username of first information class in your list.
If you want to show it in another form you can pass it as a parameter or you can make your list static (but this is not a good approach).
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.