小编Tri*_*exx的帖子

如何在C#.NET(win7手机)中使用'DataContractJsonSerializer'读入'嵌套'Json文件?

我有一个问题,如果我的json文件看起来像这样

{"Numbers":"45387","Words":"space buckets"}

我可以读得很好,但如果它看起来像这样:

{"Main":{"Numbers":"45387","Words":"space buckets"},
"Something":{"Numbers":"12345","Words":"Kransky"}}

我没有收到任何信息.我不知道如何在Main和Something之间切换!使用此代码加载带有此"嵌套"信息的JSON,

var ser = new DataContractJsonSerializer(typeof(myInfo));

var info = (myInfo)ser.ReadObject(e.Result); 
Run Code Online (Sandbox Code Playgroud)

//用于保存我的信息的类

[DataContract] 
public class myInfo 
{ 
    [DataMember(Name="Numbers")] 
    public int number 
    { get; set; } 

    [DataMember(Name="Words")] 
    public string words 
    { get; set; } 
} 
Run Code Online (Sandbox Code Playgroud)

导致类返回空.
我已经尝试将组名添加到DataContract,例如.[DataContract,Name ="Main"]但这仍然导致类值为空.
我也尝试将"main"添加到序列化器overloader,例如.var ser = new DataContractJsonSerializer(typeof(myInfo),"Main");
这会导致错误:期望元素'Main'来自命名空间''..遇到'Element',名称为'root',命名空间''.
我更喜欢使用提供的json阅读器.我已经研究过json.NET,但是发现编写json和sparse有关阅读信息的文档很重要.当然,我在这里错过了一些简单的东西!

c# silverlight json windows-phone-7 deserialization

3
推荐指数
1
解决办法
1980
查看次数

是否可以向集合中的类添加方法?

我正在尝试使用按钮和滑块在XNA中构建页面,并尝试了一些想法,但我似乎陷入了保持"面向对象"并使按钮和滑块保持有用而不会增加实际"按钮"之间的问题'和'滑块'课程.

所以我想知道是否有一种神奇的方式来实例化一个Button类,然后添加一个方法,或者某种方法的链接,以便我可以迭代我的按钮或滑块集合,如果有一个'命中'执行与该按钮相关的具体方法?

我最好在父类中一个接一个地编写方法,表示当时的屏幕绘图.

幻想代码示例:

class Room // Example class to manipulate 
{ 
    public bool LightSwitch; 
    public void Leave() { /* Leave the room */ } 
} 

class Button 
{   // Button workings in here 
    public bool GotPressed(/*click location */) 
    { /* If click location inside Rect */ return true; } 

    public void magic() {} // the method that gets overidden 
} 

public class GameScreen 
{ 
    public Room myRoom; 
    private List<Button> myButtons; 

    public GameScreen() 
    { 
        myRoom = new Room(); 
        myRoom.LightSwitch = …
Run Code Online (Sandbox Code Playgroud)

.net c# xna

2
推荐指数
1
解决办法
107
查看次数

标签 统计

c# ×2

.net ×1

deserialization ×1

json ×1

silverlight ×1

windows-phone-7 ×1

xna ×1