简体   繁体   中英

Saving json array to c# class object

I have to assign json array of string to ac# class. I have done like below. But I got an exceptio

Cannot deserialize the current JSON array (eg [1,2,3]) into type 'LoggedInUserDetails' because the type requires a JSON object (eg {\\"name\\":\\"value\\"}) to deserialize correctly.\\r\\nTo fix this error either change the JSON to a JSON object (eg {\\"name\\":\\"value\\"}) or change the deserialized type to an array or a type that implements a collection interface (eg ICollection, IList) like List that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array.\\r\\nPath '', line 1, position 1."}

public class LoggedInUserDetails
{
    public string login_user_name
    {
        get; set;
    }

    public string login_user_id
    {
        get; set;
    }
}

 var GetResponse = await response2.Content.ReadAsStringAsync();
//"[{\"login_user_name\":\"Rahul\",\"login_user_id\":\"43\"}]"
 LoggedInUserDetails obj = JsonConvert.DeserializeObject<LoggedInUserDetails>(GetResponse);
 Variables.login_user_name = obj.login_user_name;
 Variables.login_user_id= obj.login_user_id;

You should deserialize the json string to an IEnumerable collection or Array .

 var content = await response2.Content.ReadAsStringAsync();
 var result = JsonConvert.DeserializeObject<List<LoggedInUserDetails>>(content);

It because of the '[]', which stores it as a array of results. You should be able to do something like the following:

LoggedInUserDetails obj = JsonConvert.DeserializeObject<List<LoggedInUserDetails>>(GetResponse);

where you try to deserialize object as an list. The list will only contain 1 object.

full example:

public class LoggedInUserDetails
{
    public string login_user_name
    {
        get; set;
    }

    public string login_user_id
    {
        get; set;
    }
}

 var GetResponse = await response2.Content.ReadAsStringAsync();
//"[{\"login_user_name\":\"Rahul\",\"login_user_id\":\"43\"}]"
 List<LoggedInUserDetails> obj = JsonConvert.DeserializeObject<List<LoggedInUserDetails>>(GetResponse);
 Variables.login_user_name = obj[0].login_user_name;
 Variables.login_user_id= obj[0].login_user_id;

This way:

LoggedInUserDetails obj = JsonConvert.DeserializeObject<LoggedInUserDetails[]>(GetResponse).FirstOrDefault() ?? throw new Exception("User not found.");
Variables.login_user_name = obj.login_user_name;
Variables.login_user_id = obj.login_user_id;

Your result was an array containing 1 object, so you just had to convert it to an array of your desired object and take the first.

Since you are using array, then it should deserialize array to list public class Test { public string login_user_name {get; set;} public string login_user_id { get; set;} }

string js =  "[{\"login_user_name\":\"Rahul\",\"login_user_id\":\"43\"}]";
var result  =  JsonConvert.DeserializeObject<List<Test>>(js);
foreach(var ob in result)
{
  Console.WriteLine(ob.login_user_name);
  Console.WriteLine(ob.login_user_id);
}

output

Rahul
43

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