简体   繁体   English

C# JsonSerializationException 无法反序列化当前 JSON 对象

[英]C# JsonSerializationException Cannot deserialize the current JSON object

I have problem with json (i never touch json before).我对 json 有疑问(我以前从未接触过 json)。 My code is basically copy paste of this Read and parse a Json File in C# .我的代码基本上是复制粘贴这个Read and parse a Json File in C#

using (StreamReader r = new StreamReader(RandomUtils.launcherPath() + @"\users.json"))
{
    string json = r.ReadToEnd();
    List<Account> items = JsonConvert.DeserializeObject<List<Account>>(json);
} 

public class Account
{
    public string users;
    public string username;
    public string type;
    public string uuid;
    public string sessionToken;
    public string accessToken;
}

and my json (block at users)和我的json(阻止用户)

{
  "users": {
    "quinn23513@gmail.com": {
      "username": "quinn23513@gmail.com", //Just bug here ill fix later
      "type": "mojang",
      "uuid": "008d8151613d4ef4bf491520f90930c1",
      "sessionToken": "token",
      "accessToken": "Anothertoken"
    }
  }
}

I can not see any list in your json, you have to fix classes我在你的 json 中看不到任何列表,你必须修复类

 data = JsonConvert.DeserializeObject<Data>(json);

classes班级

  public class Data
{
    public Dictionary<string,Account> users { get; set; }
}

public class Account
{
    public string username { get; set; }
    public string type { get; set; }
    public string uuid { get; set; }
    public string sessionToken { get; set; }
    public string accessToken { get; set; }
}

but if you prefer a list , you can use this code但如果你喜欢一个列表,你可以使用这个代码

    List<Account> listUsers = ((JObject)JObject.Parse(json)["users"]).Properties()
                                             .Select(x => x.Value.ToObject<Account>())
                                             .ToList();

or if you have only one user或者如果您只有一个用户

Account account = ((JObject)JObject.Parse(json)["users"]).Properties()
                                 .Select(x => x.Value.ToObject<Account>())
                                 .First();

No, due to your structure List your json should be like:不,由于您的结构列表,您的 json 应该是这样的:

class SomeClass
{
  [JsonProperty("users")]
  public List<Account> Users {get;set;}
}

//And your call method should have such signature: 
var json = r.ReadToEnd();
var cl = JsonConvert.DeserializeObject<SomeClass>(json);
// use your list like:
cl.Users...

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

相关问题 JsonSerializationException:无法反序列化当前的 JSON 对象 - JsonSerializationException: Cannot deserialize the current JSON object Newtonsoft.Json.JsonSerializationException:&#39;无法反序列化当前JSON对象 - Newtonsoft.Json.JsonSerializationException: 'Cannot deserialize the current JSON object Newtonsoft.Json.JsonSerializationException:无法反序列化当前的JSON对象 - Newtonsoft.Json.JsonSerializationException: Cannot deserialize the current JSON object Json.JsonSerializationException:无法反序列化当前 JSON 对象 - Json.JsonSerializationException : Cannot deserialize the current JSON object C#无法反序列化当前的JSON对象 - c# Cannot deserialize the current JSON object 无法在C#中反序列化当前JSON对象 - Cannot deserialize the current JSON object in C# C#-无法反序列化当前JSON对象 - C# - Cannot deserialize the current JSON object 无法反序列化当前 JSON object C# - Cannot deserialize the current JSON object C# Newtonsoft.Json.JsonSerializationException: '无法反序列化当前的 JSON object (例如 {"name":"value"}) - Newtonsoft.Json.JsonSerializationException: 'Cannot deserialize the current JSON object (e.g. {"name":"value"}) Newtonsoft.Json.JsonSerializationException: &#39;无法反序列化当前的 JSON 数组 - Newtonsoft.Json.JsonSerializationException: 'Cannot deserialize the current JSON array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM