简体   繁体   English

如何正确反序列化json数据?

[英]How do I properly deserialize json data?

So I was looking through this example to try to desialize for the first time ever https://www.newtonsoft.com/json/help/html/DeserializeObject.htm I'm trying to create objects out of the json data and then get the corresponding properties. 因此,我一直在浏览此示例,以尝试首次对https://www.newtonsoft.com/json/help/html/DeserializeObject.htm进行反序列化。我试图从json数据中创建对象,然后获取相应的属性。

And well.. I got an error saying this 恩..我说错了

Newtonsoft.Json.JsonSerializationException: 'Cannot deserialize the current JSON array (eg [1,2,3]) into type 'json_deserializing.OppedAccounts' because the type requires a JSON object (eg {"name":"value"}) to deserialize correctly. Newtonsoft.Json.JsonSerializationException:'无法将当前JSON数组(例如[1,2,3])反序列化为类型'json_deserializing.OppedAccounts',因为该类型需要一个JSON对象(例如{“ name”:“ value”})正确反序列化。 To 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. 要解决此错误,可以将JSON更改为JSON对象(例如{“ name”:“ value”}),也可以将反序列化类型更改为数组,或者将实现集合接口的类型(例如ICollection,IList)更改为List,例如List从JSON数组反序列化。 JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array. 还可以将JsonArrayAttribute添加到类型中,以强制其从JSON数组反序列化。 Path '', line 1, position 1.' 路径'',第1行,位置1.'

I am new to json & deserializing so I am not sure what is going on here. 我是json和反序列化的新手,所以我不确定这里发生了什么。 My question is, how do I deserialize json in a propper way? 我的问题是,如何以适当的方式反序列化json? I read something about lists and stuff but I couldnt connect the dots. 我读了一些有关列表和东西的东西,但我无法说明问题。

[
  {
    "uuid": "98e99e7a-df48-4b8b-adc9-e65c32410247",
    "name": "UsernameOne",
    "level": 4,
    "bypassesPlayerLimit": false
  },
  {
    "uuid": "b87e1cbc-c67c-4026-a359-8652ad9de8b4",
    "name": "UsernameTwo",
    "level": 4,
    "bypassesPlayerLimit": false
  }
]

CSharp Code CSharp代码

public partial class MainWindow : Window
    {
        public string line = null;

        public MainWindow()
        {
            InitializeComponent();
            Deserialize();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if(line != null)
            {
                OppedAccounts account = JsonConvert.DeserializeObject<OppedAccounts>(line);
                Debug.Print(account.name);
            }
        }

        private void Deserialize()
        {
            using (StreamReader sr = new StreamReader("ops.json"))
            {
                line = sr.ReadToEnd();
            }
            tbjson.AppendText(line);
        }
    }

And the class 和班级

class OppedAccounts
    {
        public string uuid { get; set; }
        public string name { get; set; }
        public int level { get; set; }
        public bool bypassplayerlimit { get; set; }
    }

You are deserializing an array, so you are expecting an array of objects to be returned. 您正在反序列化一个数组,所以您期望返回一个对象数组。 Change the type of account and the generic parameter of JsonConvert.DeserializeObject to List<OppedAccounts> . account类型和JsonConvert.DeserializeObject的通用参数JsonConvert.DeserializeObjectList<OppedAccounts>

List<OppedAccounts> accounts = JsonConvert.DeserializeObject<List<OppedAccounts>>(line);

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

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