2015-07-27 3 views
-1

Как десериализовать под массивом json. не удалось получить значения field_options и массив field_options. Я использую DataContractJsonSerializer для десериализации. Мне нужно получить field_options как размер, тогда, если field_type будет выпадающим, мне понадобятся значения field_options -> options -> lebel и checked value, а также include_blank_option.Json array deserialize

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; } 
} 

большое спасибо заранее.

ответ

0

Обычно я нахожу, что запись выборочных данных в файл помогает решить эти проблемы. Я сделал классы общедоступными и установил размер дубликата в одном из классов.

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; } 
    } 
} 

​