Search code examples
c#arraysjsondatacontractserializer

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. My problem is that the object[] did not fill with data. Here is my code:

This is the serverdata.cs file with my object inside that I want to fill:

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:

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()); 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.

Here is the JSON data:

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

Solution

  • 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. To fix, you need to deserialize to your serverdata class directly. But first, you will need to make a couple of changes to the attributes:

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

    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:

    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.