Bitly、Json 和 C#
我正在做一些涉及使用 Bit.ly API 的工作,并允许用户选择文本和格式的格式(文本、XML、Json)。 XML 已完成。这是缩短 URL 时返回的 Json 结果:
{
"status_code": 200,
"status_txt": "OK",
"data":
{
"long_url": "http:\/\/panel.aspnix.com\/Default.aspx?pid={Removed}",
"url": "http:\/\/rlm.cc\/gtYUEd",
"hash": "gtYUEd",
"global_hash": "evz3Za",
"new_hash": 0
}
}
这个 C# 代码可以很好地解析它并获取短 URL:
var serializer2 = new JavaScriptSerializer();
var values2 = serializer2.Deserialize<IDictionary<string, object>>(json);
var results2 = values2["data"] as IDictionary<string, object>;
var shortUrl2 = results2["url"];
expandedUrl = results2["url"].ToString();
return results2["url"].ToString();
现在这是扩展 URL 时发回的 Json:
{
"status_code": 200,
"status_txt": "OK",
"data":
{
"expand":
[
{
"short_url": "http:\/\/rlm.cc\/gtYUEd",
"long_url": "http:\/\/panel.aspnix.com\/Default.aspx?pid={Removed}",
"user_hash": "gtYUEd",
"global_hash": "evz3Za"
}
]
}
}
这就是我的问题开始的地方,如何才能我将当前的 C# 更改为能够处理这两种情况,因为如您所见,它们彼此之间有很大不同。有什么想法吗?
I'm working on something that involved using the Bit.ly API, and allow the user to select theformat (Text, XML, Json) the text & XML are completed. This is the Json result that is returned when you shorten a URL:
{
"status_code": 200,
"status_txt": "OK",
"data":
{
"long_url": "http:\/\/panel.aspnix.com\/Default.aspx?pid={Removed}",
"url": "http:\/\/rlm.cc\/gtYUEd",
"hash": "gtYUEd",
"global_hash": "evz3Za",
"new_hash": 0
}
}
And this C# code works just fine to parse it and get the short URL:
var serializer2 = new JavaScriptSerializer();
var values2 = serializer2.Deserialize<IDictionary<string, object>>(json);
var results2 = values2["data"] as IDictionary<string, object>;
var shortUrl2 = results2["url"];
expandedUrl = results2["url"].ToString();
return results2["url"].ToString();
Now here's the Json sent back when expanding a URL:
{
"status_code": 200,
"status_txt": "OK",
"data":
{
"expand":
[
{
"short_url": "http:\/\/rlm.cc\/gtYUEd",
"long_url": "http:\/\/panel.aspnix.com\/Default.aspx?pid={Removed}",
"user_hash": "gtYUEd",
"global_hash": "evz3Za"
}
]
}
}
Ad that's where my problem begins, how can I change my current C# to be able to handle both scenarios, because as you can see their vastly different from each other. Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我通常使用 Json.NET 从 JSON 文档中挑选值。语法非常简洁。如果引用NewtonSoft.Json.dll并使用Newtonsoft.Json.Linq,则可以编写以下内容:
如果
jsonString 是:
例程将显示
http://rlm.cc/gtYUEd
。如果
jsonString
为:例程将显示
http://panel.aspnix.com/Default.aspx?pid={Removed}
。I usually use Json.NET to cherrypick values out of JSON documents. The syntax is very concise. If you reference NewtonSoft.Json.dll and use Newtonsoft.Json.Linq, you can write the following:
If
jsonString
is:the routine will display
http://rlm.cc/gtYUEd
.If
jsonString
is:the routine will display
http://panel.aspnix.com/Default.aspx?pid={Removed}
.不确定我是否明白你的问题。如果您得到缩短结果或扩展结果,为什么不进行测试呢?由于它们不同,因此可以通过简单的“if ()”语句轻松完成:
Not sure I got your problem. Why aren't you testing, if you got a shortening result or a expanding result? Since they are different, this could easily be done via simple 'if ()' statements:
假设提供者与其发送的表单一致,您是否需要拥有处理这两者的代码?应该直接单独处理每个。
如果您无法提前知道将返回哪种格式,您可以执行以下操作:
Assuming that the provider is consistent with which form it sends, do you need to have code that handles both? It should be direct to handle each individually.
If you can't know ahead of time which format you will get back, you can do the following: