Json数组反序列化
本文关键字:反序列化 数组 Json | 更新日期: 2023-09-27 18:04:54
如何反序列化json数组。无法获取field_options值和field_options数组。我正在使用DataContractJsonSerializer来反序列化。我需要获取field_options像大小,然后如果field_type是下拉,我将需要field_options -> options -> lebel和检查值以及include_blank_option的值。
string jsonString = @"[{""fields"":[{""label"":""Untitled"",""field_type"":""paragraph"",""required"":true,""field_options"":{""size"":""small""},""cid"":""c2""},{""label"":""Untitled"",""field_type"":""text"",""required"":true,""field_options"":{""size"":""small""},""cid"":""c6""},{""label"":""Untitled"",""field_type"":""dropdown"",""required"":true,""field_options"":{""options"":[{""label"":""kjhkjh"",""checked"":false},{""label"":"",hkjkhkj"",""checked"":false}],""include_blank_option"":true},""cid"":""c8""}]}]}";
DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(List<field>));
MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(jsonString));
var obj = (List<field>)ser.ReadObject(stream);
foreach (Person sp in obj[0].fields)
{
Response.Write(sp.cid + sp.field_type + sp.label + sp.required + "<BR>");
List<sizee> si = sp.field_options;
foreach (sizee sz in si)
{
Response.Write(sz.size);
}
}
[DataContract]
class field
{
[DataMember]
public List<Person> fields { get; set; }
}
[DataContract]
class Person
{
[DataMember]
public string label { get; set; }
[DataMember]
public string field_type { get; set; }
[DataMember]
public string required { get; set; }
[DataMember]
public string cid { get; set; }
[DataMember]
public List<sizee> field_options { get; set; }
}
[DataContract]
class sizee
{
[DataMember]
public string size { get; set; }
[DataMember]
public List<option> size { get; set; }
}
[DataContract]
class option
{
[DataMember]
public string checke { get; set; }
[DataMember]
public string label { get; set; }
}
提前感谢。
我通常发现将示例数据写入文件有助于解决这些类型问题。我将这些类设为公共,并在其中一个类中固定了一个重复的属性大小。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using System.IO;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME = @"c:'temp'test.txt";
static void Main(string[] args)
{
field f = new field()
{
fields = new List<Person>() {
new Person() {
cid = "abc",
field_type = "def",
label = "ghi",
required = "true",
field_options = new List<sizee>() {
new sizee() {
size = "AAA",
options = new List<option>() {
new option() {
checke = "123",
label = "456",
},
new option() {
checke = "789",
label = "012",
}
}
},
new sizee() {
size = "BBB",
options = new List<option>() {
new option() {
checke = "321",
label = "654",
},
new option() {
checke = "987",
label = "210",
}
}
}
}
},
new Person() {
cid = "xyz",
field_type = "def",
label = "ghi",
required = "true",
field_options = new List<sizee>() {
new sizee() {
size = "AAA",
options = new List<option>() {
new option() {
checke = "123",
label = "456",
},
new option() {
checke = "789",
label = "012",
}
}
},
new sizee() {
size = "BBB",
options = new List<option>() {
new option() {
checke = "321",
label = "654",
},
new option() {
checke = "987",
label = "210",
}
}
}
}
}
}
};
DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(field));
FileStream stream = File.OpenWrite(FILENAME);
ser.WriteObject(stream, f);
stream.Flush();
stream.Close();
stream.Dispose();
}
}
[DataContract]
public class field
{
[DataMember]
public List<Person> fields { get; set; }
}
[DataContract]
public class Person
{
[DataMember]
public string label { get; set; }
[DataMember]
public string field_type { get; set; }
[DataMember]
public string required { get; set; }
[DataMember]
public string cid { get; set; }
[DataMember]
public List<sizee> field_options { get; set; }
}
[DataContract]
public class sizee
{
[DataMember]
public string size { get; set; }
[DataMember]
public List<option> options { get; set; }
}
[DataContract]
public class option
{
[DataMember]
public string checke { get; set; }
[DataMember]
public string label { get; set; }
}
}