2014-08-08 4 views
1

Это мой JSON файла:Populate JSON в PropertyGrid

{ 
"properties": [ 
    { 
     "name": "Text", 
     "value": "", 
     "default": "", 
     "type": "string", 
     "desc": "The text associated with the control." 
    }, 
    { 
     "name": "Items", 
     "default": "item1", 
     "items": ["item1","item2","item3"], 
     "type": "list", 
     "desc": "List of items." 
    }, 
    { 
     "name": "Pages", 
     "type": "collection", 
     "desc": "List of items.", 
     "properties": [ 
       { 
        "name": "Text", 
        "value": "", 
        "default": "", 
        "type": "string", 
        "desc": "The page text." 
       }    
     ], 
     "items": [ 
       { 
        "Text": "page1" 
       }, 
       { 
        "Text": "page2" 
       } 
     ] 
    } 
] 
} 

Что бы быть лучшим подходом для динамического заполнения сетки свойств на основе файла JSON (с помощью Json.NET)?

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

благодаря

+0

Вы можете использовать JObject.parse («Json string») для достижения этого, следующий раздел, который вы можете указать: http: //james.newtonking.com/json/help/index.html; http://weblog.west-wind.com/posts/2012/Aug/30/Using-JSONNET-for-dynamic-JSON-parsing – holmes2136

ответ

2

Что вы можете сделать, это использовать custom type descripto г, как я показываю здесь. Вот как это выглядит в стандартном WinForm:

enter image description here

... 
    propertyGrid1.SelectedObject = new JTypeDescriptor(JObject.Parse(File.ReadAllText("test.json"))); 
    ... 

    public class JTypeDescriptor : ICustomTypeDescriptor 
    { 
     public JTypeDescriptor(JObject jobject) 
     { 
      if (jobject == null) 
       throw new ArgumentNullException("jobject"); 

      JObject = jobject; 
     } 

     // NOTE: the property grid needs at least one r/w property otherwise it will not show properly in collection editors... 
     public JObject JObject { get; set; } 

     public override string ToString() 
     { 
      // we display this object's serialized json as the display name, for example 
      return JObject.ToString(Formatting.None); 
     } 

     PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties(Attribute[] attributes) 
     { 
      // browse the JObject and build a list of pseudo-properties 
      List<PropertyDescriptor> props = new List<PropertyDescriptor>(); 
      foreach (var kv in JObject) 
      { 
       props.Add(new Prop(kv.Key, kv.Value, null)); 
      } 
      return new PropertyDescriptorCollection(props.ToArray()); 
     } 

     AttributeCollection ICustomTypeDescriptor.GetAttributes() 
     { 
      return null; 
     } 

     string ICustomTypeDescriptor.GetClassName() 
     { 
      return null; 
     } 

     string ICustomTypeDescriptor.GetComponentName() 
     { 
      return null; 
     } 

     TypeConverter ICustomTypeDescriptor.GetConverter() 
     { 
      return null; 
     } 

     EventDescriptor ICustomTypeDescriptor.GetDefaultEvent() 
     { 
      throw new NotImplementedException(); 
     } 

     PropertyDescriptor ICustomTypeDescriptor.GetDefaultProperty() 
     { 
      return null; 
     } 

     object ICustomTypeDescriptor.GetEditor(Type editorBaseType) 
     { 
      return null; 
     } 

     EventDescriptorCollection ICustomTypeDescriptor.GetEvents(Attribute[] attributes) 
     { 
      throw new NotImplementedException(); 
     } 

     EventDescriptorCollection ICustomTypeDescriptor.GetEvents() 
     { 
      throw new NotImplementedException(); 
     } 

     PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties() 
     { 
      return ((ICustomTypeDescriptor)this).GetProperties(null); 
     } 

     object ICustomTypeDescriptor.GetPropertyOwner(PropertyDescriptor pd) 
     { 
      return this; 
     } 

     // represents one dynamic pseudo-property 
     private class Prop : PropertyDescriptor 
     { 
      private Type _type; 
      private object _value; 

      public Prop(string name, object value, Attribute[] attrs) 
       : base(name, attrs) 
      { 
       _value = ComputeValue(value); 
       _type = _value == null ? typeof(object) : _value.GetType(); 
      } 

      private static object ComputeValue(object value) 
      { 
       if (value == null) 
        return null; 

       JArray array = value as JArray; 
       if (array != null) 
       { 
        // we use the arraylist because that's all the property grid needs... 
        ArrayList list = new ArrayList(); 
        for (int i = 0; i < array.Count; i++) 
        { 
         JObject subo = array[i] as JObject; 
         if (subo != null) 
         { 
          JTypeDescriptor td = new JTypeDescriptor(subo); 
          list.Add(td); 
         } 
         else 
         { 
          JValue jv = array[i] as JValue; 
          if (jv != null) 
          { 
           list.Add(jv.Value); 
          } 
          else 
          { 
           // etc. 
          } 
         } 
        } 
        // we don't support adding things 
        return ArrayList.ReadOnly(list); 
       } 
       else 
       { 
        // etc. 
       } 
       return value; 
      } 

      public override bool CanResetValue(object component) 
      { 
       return false; 
      } 

      public override Type ComponentType 
      { 
       get { return typeof(object); } 
      } 

      public override object GetValue(object component) 
      { 
       return _value; 
      } 

      public override bool IsReadOnly 
      { 
       get { return false; } 
      } 

      public override Type PropertyType 
      { 
       get { return _type; } 
      } 

      public override void ResetValue(object component) 
      { 
      } 

      public override void SetValue(object component, object value) 
      { 
       _value = value; 
      } 

      public override bool ShouldSerializeValue(object component) 
      { 
       return false; 
      } 
     } 
    } 

PS: Я не очень знаком с Json.NET, и, кажется, есть некоторые JPropertyDescriptor, но я не кажется, действительно свойство сетки подходящее.

+0

спасибо, я могу работать от этого, чтобы развить его так, как он мне нужен. – user2909323