简体   繁体   English

使用DataContractJsonSerializer从JSON数据填充对象数组时出错

[英]Error filling an object array from JSON data using DataContractJsonSerializer

I have one problem. 我有一个问题。 I want to read JSON data from my local link and put it in an object class. 我想从本地链接读取JSON数据并将其放在对象类中。 My problem is that the object[] did not fill with data. 我的问题是object []没有填充数据。 Here is my code: 这是我的代码:

This is the serverdata.cs file with my object inside that I want to fill: 这是serverdata.cs文件,其中包含我要填充的对象:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.Serialization;
namespace Διαχείριση
{
    class serverdata
    {

        public προμηθευτέςRow[] Rows;
        [DataContract(Name = "ΠρομηθευτέςResult")]
        public struct προμηθευτέςRow
        {
            [DataMember(Name = "Κωδικός")]
            public int Κωδικός { get; set; }
            [DataMember(Name = "Όνομα")]
            public string Όνομα { get; set; }
            [DataMember(Name = "Επίθετο")]
            public string Επίθετο { get; set; }
            [DataMember(Name = "Τηλέφωνο")]
            public string Τηλέφωνο { get; set; }
            [DataMember(Name = "Διεύθυνση")]
            public string Διεύθυνση { get; set; }
            [DataMember(Name = "Mail")]
            public string Mail { get; set; }
            [DataMember(Name = "Προϊόντα")]
            public string[] Προϊόντα { get; set; }
        }
    }
}

Then I have the Form.cs that I want to read the JSON data from my local server: 然后,我有了想要从本地服务器读取JSON数据的Form.cs:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.Serialization.Json;
namespace Διαχείριση
{
    public partial class Administator_Form : Form
    {
        serverdata ServerData;
        public Administator_Form()
        {
            ServerData = new serverdata(); 
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            WebRequest request = WebRequest.Create(string.Format("mylocallink"));
            WebResponse response = request.GetResponse();
            Stream stream = request.GetResponse().GetResponseStream();
             StreamReader sread = new StreamReader(stream);
             //string sLine = sread.ReadLine();
             //MessageBox.Show(sLine);
            DataContractJsonSerializer json = new DataContractJsonSerializer(typeof(List<serverdata.προμηθευτέςRow>));
            var result = (List<serverdata.προμηθευτέςRow>)json.ReadObject(stream);
            ServerData.Rows = result.ToArray();
        }
    } 
}

Now if I call for example MessageBox.Show(ServerData.Rows[0].Κωδικός.ToString()); 现在,如果我打电话给MessageBox.Show(ServerData.Rows[0].Κωδικός.ToString()); I get an exception: 我有一个例外:

"An unhandled exception of type 'System.IndexOutOfRangeException' occurred in Project.exe
Additional information: Index was outside the bounds of the array."

So my problem is that result didn't fill ServerData.Rows . 所以我的问题是result没有填充ServerData.Rows

Here is the JSON data: 这是JSON数据:

{
    "ΠρομηθευτέςResult": [
        {
            "Mail": "mail1",
            "Όνομα": "name1",
            "Διεύθυνση": "address1",
            "Επ‌​ίθετο": "epitheto1",
            "Κωδικός": 1,
            "Προϊόντα": [
                "subproduct1.1",
                "subproduct1.2"
            ],
            "Τηλέ‌​φωνο": "1111111111"
        },
        {
            "Mail": "mail2",
            "Όνομα": "name2",
            "Διεύθυνση": "address2",
            "Επίθε‌​το": "epitheto2",
            "Κωδικός": 2,
            "Προϊόντα": [
                "subproduct2.1",
                "subproduct2.2"
            ],
            "Τηλέφων‌​ο": "2222222222"
        }
    ]
}

The issue is that you are trying to deserialize into a list, but in your JSON the row data is not at the root level--it is inside an object. 问题是您试图反序列化到列表中,但是在JSON中,行数据不在根级别上,而是在对象内部。 To fix, you need to deserialize to your serverdata class directly. 要修复,您需要直接反序列化到serverdata类。 But first, you will need to make a couple of changes to the attributes: 但是首先,您需要对属性进行一些更改:

  1. Mark your serverdata class with [DataContract] [DataContract]标记您的serverdata
  2. Mark the Rows property inside serverdata with [DataMember(Name = "ΠρομηθευτέςResult")] 标记Rows内属性serverdata[DataMember(Name = "ΠρομηθευτέςResult")]
  3. Mark the προμηθευτέςRow struct with [DataContract] [DataContract]标记προμηθευτέςRow结构

Your class should look like this: 您的课程应如下所示:

[DataContract]
class serverdata
{
    [DataMember(Name = "ΠρομηθευτέςResult")]
    public προμηθευτέςRow[] Rows { get; set; }

    [DataContract]
    public struct προμηθευτέςRow
    {
        [DataMember(Name = "Κωδικός")]
        public int Κωδικός { get; set; }
        [DataMember(Name = "Όνομα")]
        public string Όνομα { get; set; }
        [DataMember(Name = "Επίθετο")]
        public string Επίθετο { get; set; }
        [DataMember(Name = "Τηλέφωνο")]
        public string Τηλέφωνο { get; set; }
        [DataMember(Name = "Διεύθυνση")]
        public string Διεύθυνση { get; set; }
        [DataMember(Name = "Mail")]
        public string Mail { get; set; }
        [DataMember(Name = "Προϊόντα")]
        public string[] Προϊόντα { get; set; }
    }
}

Then, change your code to deserialize to your serverdata class: 然后,更改代码以反序列化为serverdata类:

DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(serverdata));
ServerData = (serverdata)ser.ReadObject(stream);

You can remove this line as it is no longer needed: 您可以删除此行,因为它不再需要:

ServerData.Rows = result.ToArray();

After these changes you should find that the Rows array is filled correctly. 完成这些更改后,您应该发现Rows数组已正确填充。

暂无
暂无

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

相关问题 使用DataContractJsonSerializer作为JSON数组序列化对象 - Serialize an object using DataContractJsonSerializer as a json array 如何使用DataContractJsonSerializer使用从Web(以JSON形式)获取的数据设置对象属性的子集 - How to set a subset of properties of an object using data obtained from web (in form of JSON) using DataContractJsonSerializer 无法使用DataContractJsonSerializer将对象序列化为JSON - Unable to Serialize Object to JSON Using DataContractJsonSerializer 使用DataContractJsonSerializer设置JSON对象root - Set JSON object root using DataContractJsonSerializer 使用 DataContractJsonSerializer 将字典序列化为 JSON 对象 - Serialize a Dictionary as JSON object using DataContractJsonSerializer 在C#中使用DataContractJsonSerializer反序列化JSON对象 - Deserialization of JSON object by using DataContractJsonSerializer in C# 使用DataContractJsonSerializer对JSON对象进行部分反序列化 - Partial deserialization of JSON object by using DataContractJsonSerializer 使用DataContractJsonSerializer将元素附加到文件中的JSON数组 - Appending elements to a JSON array in a file using DataContractJsonSerializer 使用DataContractJsonSerializer使用不同类型的数组反序列化json - Deserialize json with array of different types using DataContractJsonSerializer 使用DataContractJsonSerializer反序列化变量Type JSON数组 - Deserializing variable Type JSON array using DataContractJsonSerializer
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM