2015-07-27 2 views
-1

Comment désérialiser sous tableau json. impossible d'extraire les valeurs field_options et le tableau de field_options. J'utilise DataContractJsonSerializer pour désérialiser. Je besoin d'aller chercher les field_options comme la taille alors si field_type est menu déroulant je vais les valeurs nécessaires de field_options -> Options -> LeBel et valeur vérifiée ainsi que 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; } 
} 

un grand merci à l'avance.

Répondre

0

Je trouve habituellement l'écriture de données d'échantillons dans un fichier permet de résoudre ces problèmes de type. J'ai rendu les classes publiques et ai fixé une taille de propriété en double dans l'une des classes.

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

​