Json数组反序列化
Posted
技术标签:
【中文标题】Json数组反序列化【英文标题】:Json array deserialize 【发布时间】:2015-10-18 08:03:03 【问题描述】:如何反序列化下面的 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;
非常感谢。
【问题讨论】:
【参考方案1】:我通常发现将示例数据写入文件有助于解决这些类型问题。我公开了这些类,并在其中一个类中修复了重复的属性大小。
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;
【讨论】:
以上是关于Json数组反序列化的主要内容,如果未能解决你的问题,请参考以下文章
JSON PHP中,Json字符串反序列化成对象/数组的方法