解析c#中的JSON字符串

时间:2015-08-03 18:43:16

标签: c# json json.net

好的,有一些关于这个的帖子......我还没有让他们上班。

我有一个简单的对象

{
    "Projects": "Projects",
    "Dashboard": "Dashboard",
    "Applications": "Applications",
    "Plans": "Plans",
    "Logout": "Logout"
}

我正在尝试反序列化它。我有牛顿软件。

这就是我所做的不起作用。

var languageString = "{"Projects":"Projects","Dashboard":"Dashboard","Applications":"Applications","Plans":"Plans","Logout":"Logout"}";
var objOut = new { name = "", lastname = "" };
var result = JsonConvert.DeserializeAnonymousType(languageString, objOut);

我一直在匿名对象上收到错误。

任何帮助都将不胜感激。

由于

4 个答案:

答案 0 :(得分:3)

要反序列化对象,需要确保用于反序列化它们的类或匿名类型与源JSON字符串匹配。

如果你有一个JSON { "name": "john", "lastname": "smith" },你的代码应该可以工作:

// I expect no one to have concerns about the double quots (""): it's
// another way of escaping quots in C# (you should also use \" but it's
// uglier, isn't it? ;)
string jsonText = @"{ ""name"": ""john"", ""lastname"": ""smith"" }";
var sampleType = new { name = "", lastname = "" };
var deserializedObject = JsonConvert.DeserializeAnonymousType(jsonText, sampleType );

在你的情况下......

...您需要一个样本匿名类型实例,如下所示:

var sampleType = new 
{
    Projects = "",
    Dashboard = "",
    Applications = "",
    Plans = "",
    Logout = ""
}

......或者使用动态对象怎么样?

您还可以使用dynamic反序列化对象ExpandoObject,您将不需要样本类型!

dynamic deserializedObject = 
       JsonConvert.DeserializeObject(languageString, new ExpandoObjectConverter());

请记住,这是 duck typing :在运行时之前,您不知道反序列化对象是否具有预期的属性。 BTW,记录良好的代码可以解决问题,如果您希望反序列化的对象具有lastname之类的属性,您可以使用deserializedObject.lastname直接访问它!

答案 1 :(得分:1)

在我看来,通常更容易反序列化为强类型的类。鉴于您提供的JSON,您的反序列化类将如下所示:

public class RootObject
{
    public string Projects { get; set; }
    public string Dashboard { get; set; }
    public string Applications { get; set; }
    public string Plans { get; set; }
    public string Logout { get; set; }
}

虽然这个JSON相当简单,但如果它更复杂,你可以使用http://json2csharp.com从JSON自动生成C#类。

然后您需要做的就是:

var languageString = "{\"Projects\":\"Projects\",\"Dashboard\":\"Dashboard\",\"Applications\":\"Applications\",\"Plans\":\"Plans\",\"Logout\":\"Logout\"}";
var result = JsonConvert.DeserializeObject<RootObject>(languageString);

答案 2 :(得分:1)

 var languageString = @"{""Projects"":""Projects"",""Dashboard"":""Dashboard"",""Applications"":""Applications"",""Plans"":""Plans"",""Logout"":""Logout""}";

        var objOut = new { Projects = "", Dashboard = "", Applications = "", Plans = "", Logout = "" };
        var result = JsonConvert.DeserializeAnonymousType(languageString, objOut);

这对你有用。

使用您的代码我没有收到任何错误。请详细解释您的错误

答案 3 :(得分:1)

我在JSON字符串中看不到 name lastname 参数,所以我不确定你要用它做什么。此外,如果您愿意实际编译代码,您应该可以转义引号。你可以使用逐字字符串(@)和双引号或反斜杠。

逐字字符串:

var languageString = @"{""Projects"":""Projects"",""Dashboard"":""Dashboard"",""Applications"":""Applications"",""Plans"":""Plans"",""Logout"":""Logout""}";

<强>反斜杠:

var languageString = "{\"Projects\":\"Projects\",\"Dashboard\":\"Dashboard\",\"Applications\":\"Applications\",\"Plans\":\"Plans\",\"Logout\":\"Logout\"}";

现在当你正确地使用该字符串时,如果你愿意反序列化它,你应该创建一个与你的JSON匹配的正确类。您的匿名类应该如下所示:

var objOut = new
{
    Projects = "",
    Dashboard = "",
    Applications = "",
    Plans = "",
    Logout = ""
};

完成所有操作后,您将在匿名类的对象实例中检索JSON值。