【发布时间】:2014-10-15 21:15:22
【问题描述】:
我有一个问题。我想从我的本地链接读取 JSON 数据并将其放入对象类中。我的问题是 object[] 没有填充数据。这是我的代码:
这是我要填充的包含我的对象的 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; }
}
}
}
然后我有我想从本地服务器读取 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();
}
}
}
现在如果我打电话给例如MessageBox.Show(ServerData.Rows[0].Κωδικός.ToString()); 我会得到一个异常:
"An unhandled exception of type 'System.IndexOutOfRangeException' occurred in Project.exe
Additional information: Index was outside the bounds of the array."
所以我的问题是result 没有填写ServerData.Rows。
这是 JSON 数据:
{
"ΠρομηθευτέςResult": [
{
"Mail": "mail1",
"Όνομα": "name1",
"Διεύθυνση": "address1",
"Επίθετο": "epitheto1",
"Κωδικός": 1,
"Προϊόντα": [
"subproduct1.1",
"subproduct1.2"
],
"Τηλέφωνο": "1111111111"
},
{
"Mail": "mail2",
"Όνομα": "name2",
"Διεύθυνση": "address2",
"Επίθετο": "epitheto2",
"Κωδικός": 2,
"Προϊόντα": [
"subproduct2.1",
"subproduct2.2"
],
"Τηλέφωνο": "2222222222"
}
]
}
【问题讨论】:
-
您可以将实际的 JSON 作为文本放入您的问题中,而不是发布一些查看者对 JSON 的解释的 jpg 图像吗?这将使我们更容易为您提供帮助。
-
{"ΠρομηθευτέςResult":[{"Mail":"mail1","Όνομα":"name1","Διεύθυνση":"address1","Επίθετο":"epitheto1","Κωδικός" :1,"Προϊόντα":["subproduct1.1","subproduct1.2"],"Τηλέφωνο":"1111111111"},{"Mail":"mail2","Όνομα":"name2","Διεύθυνση" :"address2","Επίθετο":"epitheto2","Κωδικός":2,"Προϊόντα":["subproduct2.1","subproduct2.2"],"Τηλέφωνο":"2222222222"}]}
-
感谢您添加 JSON。我已经添加了一个答案,希望对您有所帮助。
标签: c# arrays json datacontractserializer