使用Json.NET在Windows Phone 7中反序列化JSON [英] Deserializing JSON in Windows Phone 7 with Json.NET

查看:92
本文介绍了使用Json.NET在Windows Phone 7中反序列化JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道对此有无数问题.我读了很多,但很少了解.您能否帮助阐明WP7中反序列化JSON的过程?

I know that there are countless questions about this. I've read many of them, but to little understanding. Can you help clarify the process of deserializing JSON in WP7?

我有一个看起来像这样的JSON:

I've got JSON that looks like this:

{ 
    "status" : {
        "code" : 99 ,
        "message" : "Already checked in" 
    },

    "response" : {
        "token" : "faisdufhdaisuflasdkf",
        "distance" : 20,
        "angle" : 45
    }   
}

我正在尝试使用Json.NET,但这对我的理解还很薄弱.

I am trying to use Json.NET, but this is where my understanding is pretty much naught.

 var deserializedJSON = JsonConvert.DeserializeObject<Dictionary<string, <TYPE> >>(JsonString);

对于<TYPE>,如何最好地定义期望的反序列化对象?状态和响应作为单独的类?还是只有一个通用的全封装ServerResponse类?

For <TYPE>, how can I best define my expected deserialized object? Status and Response as separate classes? Or just one generic all-encapsulating ServerResponse class?

此外,我怎么知道该序列化程序会将正确的输出分配给正确的类成员变量?如果我有

Also, how do I know that this serializer will assign the correct output to the correct class member variables? if I have

class Status {
    string code;
    string message;
}

我怎么知道这些将被正确分配?

How do I know those will be assigned properly?

谢谢.道歉,如果这似乎是微不足道的.这是我在C#,Silverlight,Windows Phone 7和/或.NET中的第一个项目.

Thanks. Apologies if this seems trivial. This is my very first project in C#, Silverlight, Windows Phone 7, and/or .NET

推荐答案

{ 
    "status" : {
        "code" : 99 ,
        "message" : "Already checked in" 
    },

    "response" : {
        "token" : "faisdufhdaisuflasdkf",
        "distance" : 20,
        "angle" : 45
    }   
}

翻译为

public class item {
    public status status { get; set; }
    public response response { get; set; }
}
public class status {
    public int code { get; set; }
    public string message { get; set; }
}
public class response {
    public string token { get; set; }
    public int distance { get; set; }
    public int angle { get; set; }
}

但是在这种情况下,项目是匿名的(仍然有效)

but in this case item is anonymous (which is still valid)

然后像这样使用它:

var deserializedJSON = JsonConvert.DeserializeObject<item>(JsonString);

这篇关于使用Json.NET在Windows Phone 7中反序列化JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆