如何解析不JSON.NET库JSON? [英] How to parse JSON without JSON.NET library?

查看:163
本文介绍了如何解析不JSON.NET库JSON?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想生成适用于Windows 8的Visual Studio 2011的一个Metro应用。
虽然我试图做到这一点,我有关于如何解析一些问题 JSON 没有 JSON.NET 库(它不支持城域应用还)。

不管怎样,我要分析此:

  {
   名:白马王子
   艺术家:Metallica的
   流派:摇滚和重金属
   专辑:刷新,
   album_image:HTTP:\\ / \\ / up203.siz.co.il \\ / UP2 \\ /u2zzzw4mjayz.png
   链接:HTTP:\\ / \\ / f2h.co.il \\ / 7779182246886
}


解决方案

您可以使用在的 System.Json命名空间其添加在.NET 4.5。

借助 JsonValue .Parse()方法解析JSON文本,并返回一个的 JsonValue

  JsonValue值= JsonValue.Parse(@{名:白马王子......);

如果您通过一个JSON对象的字符串,你应该能值转换为的的JSONObject

  JSONObject的结果=值的JSONObject;Console.WriteLine(名称.... {0}(字符串)结果[名称]);
Console.WriteLine(艺术家.. {0}(字符串)结果[艺术家]);
Console.WriteLine(类型... {0}(字符串)结果[流派]);
Console.WriteLine(专辑... {0}(字符串)结果[专辑]);

中的类很相似的系统发现。 Xml.Linq命名空间

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"
}

解决方案

You can use the classes found in the System.Json Namespace which were added in .NET 4.5.

The JsonValue.Parse() Method parses JSON text and returns a JsonValue:

JsonValue value = JsonValue.Parse(@"{ ""name"":""Prince Charming"", ...");

If you pass a string with a JSON object, you should be able to cast the value to a JsonObject:

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"]);

The classes are quite similar to those found in the System.Xml.Linq Namespace.

这篇关于如何解析不JSON.NET库JSON?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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