2012-08-12 185 views
2

我需要这方面的帮助:反序列化JSON对象

我的JSON(这是一个从暗黑破坏神3 API):

{ 
    "name":"Exsanguinating Chopsword of Assault", 
    "icon":"mightyweapon1h_202", 
    "displayColor":"blue", 
    "tooltipParams":"item-data/COGHsoAIEgcIBBXIGEoRHYQRdRUdnWyzFB2qXu51MA04kwNAAFAKYJMD", 
    "requiredLevel":60, 
    "itemLevel":61, 
    "bonusAffixes":0, 
    "dps":{ 
     "min":206.69999241828918, 
     "max":206.69999241828918 
    } 
} 

这不是完整的JSON,但我试图解析仅此因为我正在学习它。

我知道如何获取字符串名称,图标,displayColor .....但我不知道如何获得DPS。

我的模型类:

namespace Diablo_III_Profile 
{ 
[DataContract] 
public class ItemInformation : INotifyPropertyChanged 
{ 
    private string _name; 

    [DataMember] 
    public string name 
    { 
     get 
     { 
      return _name; 
     } 
     set 
     { 
      if (value != _name) 
      { 
       _name = value; 
       NotifyPropertyChanged("name"); 
      } 
     } 
    } 
    //others strings and ints here 

    public event PropertyChangedEventHandler PropertyChanged; 
    private void NotifyPropertyChanged(String propertyName) 
    { 
     PropertyChangedEventHandler handler = PropertyChanged; 
     if (null != handler) 
     { 
      handler(this, new PropertyChangedEventArgs(propertyName)); 
     } 
    } 
} 

什么是DPS的“格式”把我的模型类?

要读我使用这个字符串:

MemoryStream ms = new MemoryStream(); 
ItemInformation data = (ItemInformation)Deserialize(ms, typeof(ItemInformation)); 
MessageBox.Show(data.name); 

应该是一样的DPS?

编辑:

我知道了!如果是说不上来的最佳方式,但....

在我的模型类我把

public class DPS 
    { 
     public float min { get; set; } 
     public float max { get; set; } 
    } 

    private DPS _dps; 

    [DataMember] 
    public DPS dps 
    { 
     get 
     { 
      return _dps; 
     } 
     set 
     { 
      if (value != _dps) 
      { 
       _dps = value; 
       NotifyPropertyChanged("dps"); 
      } 
     } 
    } 
+0

,你能解决问题真棒@阿德尔莫佩雷拉,但你可以用你的解决方案回答你的问题吗?它会让其他人更容易找到解决方案,这就是我们希望Sack Overflow的方式。谢谢:) – Daniel 2012-08-12 01:39:26

+0

谢谢@Daniel!它是我第一次使用Stackoverflow来提出问题。 – 2012-08-13 16:38:52

+0

这很酷,欢迎在这里:)希望你得到你想要的一切,并帮助其他人:) – Daniel 2012-08-13 17:01:46

回答

0

你的解决方案是很好的,这正是我试图与答复。通过这种方式,您可以随时重复使用DPS,并且JSON解析器应该正确地将其反序列化为“ItemInformation”,其DPS最小值和最大值的值为正确值。我使用了相同的方法来填充对象和子对象。

您还可以使用JSON.Net进行序列化为JSON和反序列化JSON对象。

http://james.newtonking.com/projects/json/help/index.html

干杯

0

我做到了!说不上来,如果是最好的方式,但....

在我的模型类我把

public class DPS 
{ 
    public float min { get; set; } 
    public float max { get; set; } 
} 

private DPS _dps; 

[DataMember] 
public DPS dps 
{ 
    get 
    { 
     return _dps; 
    } 
    set 
    { 
     if (value != _dps) 
     { 
      _dps = value; 
      NotifyPropertyChanged("dps"); 
     } 
    } 
}