如何在没有 JSON.NET 库的情况下解析 JSON? [英] How to parse JSON without JSON.NET library?
问题描述
我正在尝试在 Visual Studio 2011 上为 Windows 8 构建 Metro 应用程序.当我尝试这样做时,我在如何在没有 JSON.NET
库的情况下解析 JSON
时遇到了一些问题(它还不支持 Metro 应用程序)).
I'm trying to build a Metro application for Windows 8 on Visual Studio 2011.
and while I'm trying to do that, I'm having some issues on how to parse JSON
without JSON.NET
library (It doesn't support the metro applications yet).
无论如何,我想解析这个:
Anyway, I want to parse this:
{
"name":"Prince Charming",
"artist":"Metallica",
"genre":"Rock and Metal",
"album":"Reload",
"album_image":"http://up203.siz.co.il/up2/u2zzzw4mjayz.png",
"link":"http://f2h.co.il/7779182246886"
}
推荐答案
您可以使用 System.Json 命名空间.您需要添加对 System.Runtime.Serialization 程序集
You can use the classes found in the System.Json Namespace which were added in .NET 4.5. You need to add a reference to the System.Runtime.Serialization assembly
JsonValue.Parse() 方法 解析 JSON 文本并返回 JsonValue:
The JsonValue.Parse() Method parses JSON text and returns a JsonValue:
JsonValue value = JsonValue.Parse(@"{ ""name"":""Prince Charming"", ...");
如果您传递带有 JSON 对象的字符串,您应该能够将该值转换为 JsonObject:
If you pass a string with a JSON object, you should be able to cast the value to a JsonObject:
using System.Json;
JsonObject result = value as JsonObject;
Console.WriteLine("Name .... {0}", (string)result["name"]);
Console.WriteLine("Artist .. {0}", (string)result["artist"]);
Console.WriteLine("Genre ... {0}", (string)result["genre"]);
Console.WriteLine("Album ... {0}", (string)result["album"]);
这篇关于如何在没有 JSON.NET 库的情况下解析 JSON?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!