帮助在 .NET 中解析 Bit.ly 的 JSON 结果
来自 .NET 程序集(非 Web 应用程序)...
Bit.ly 的正常响应如下所示。建议使用什么方式使用该结果,以便我可以轻松获取 ShortUrl 字段的值?由于原始 URL 作为“键”返回,因此构建模型类以将其反序列化并使用 LINQ 似乎没有意义。在 Javascript 中,一个简单的 .eval 就可以工作,但是由于模型是动态的,在 .NET 中推荐的方法是什么?
{
"errorCode": 0,
"errorMessage": "",
"results":
{
"http://www.google.com/":
{
"hash": "xxxxxx",
"shortKeywordUrl": "",
"shortUrl": "http://bit.ly/xxxxx",
"userHash": "1F5ewS"
}
},
"statusCode": "OK"
}
From a .NET assembly (non-web app)...
The normal response from Bit.ly is somewhat in the form of below. What is recommended way of consuming that result so that I can easily get the value of the shortUrl field? Since the original URL comes back as a "key", building a model class to deserialize it to and using LINQ does not seem to make sense. In Javascript, a simple .eval would work but what is the recommended approach in .NET since the model would be dynamic?
{
"errorCode": 0,
"errorMessage": "",
"results":
{
"http://www.google.com/":
{
"hash": "xxxxxx",
"shortKeywordUrl": "",
"shortUrl": "http://bit.ly/xxxxx",
"userHash": "1F5ewS"
}
},
"statusCode": "OK"
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
.NET 提供了类似于 eval 的机制 (JavaScriptSerializer )。如果您只需要解析出一些值,则代码将如下所示:
如果您要访问其他数据,您可以创建自己的 DTO 并让序列化程序将 JSON 数据映射到该数据。
.NET provides a mechanism similar to eval (JavaScriptSerializer). If you just need to parse out a few values the code would look like this:
If you'll be accessing the other data, you can create your own DTO and have the serializer map the JSON data to that.