Click here to Skip to main content
15,889,931 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
When i Convert string To Json Object Then Error This type of Error Show.(
Cannot deserialize the current json object because(e.g.{"name":"value"}) into type because the type requires a json array (e.g.[1,2,3])



Json String......
{
  "success": 1,
  "login": [
    {
      "username": "123456",
      "password": "123456",
      "store_authority": "Yes",
      "fyear": "2018-19"
    }
  ]
}


What I have tried:

string Post_Status = "http://123456.com/webapi/login.php?username=" + username + "&password=" + password + "";
                WebClient webClient = new WebClient();
                webClient.Headers.Add("user-agent", "Only a test!");
                string json = webClient.DownloadString(Post_Status);
                var model = JsonConvert.DeserializeObject<List<RootObject>>(json);

public class RootObject
        {
            public string success { get; set; }
            public List<Login> login { get; set; }
        }

public class Login
        {
            public string username { get; set; }
            public string password { get; set; }		
            public string store_authority { get; set; }		
            public string fyear { get; set; }		
        }
Posted
Updated 19-May-22 2:46am
v3
Comments
Richard MacCutchan 15-Oct-18 9:01am    
Are you seriously posting your password in clear text direct from your application?
Sabhani Vipul 16-Oct-18 1:28am    
this record is not real only show for solution...

The following Json
{
  "success": 1,
  "login": [
    {
      "username": "123456",
      "password": "123456",
      "store_authority": "Yes",
      "fyear": "2018-19"
    }
  ]
}

Is not an enumerable of "Root Objects. It contains a list but isn't one.

The correct way to deserialize it is:
C#
var model = JsonConvert.DeserializeObject<RootObject>(json);


To be valid JSON for your desrializer it would have to look like this:
[{
  "success": 1,
  "login": [
    {
      "username": "123456",
      "password": "123456",
      "store_authority": "Yes",
      "fyear": "2018-19"
    }
  ]
}]

Note the extra square brackets

Hope that helps
Andy
 
Share this answer
 
JsonConvert.DeserializeObject<List<RootObject>>(json);


You are deserialising the json into an array of RootObject objects, however the json don't contain an array of those items, only one, so deserialise to just RootObject, you don't need the List.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900