Bitly、Json 和 C#

发布于 2024-10-31 02:50:04 字数 1229 浏览 0 评论 0原文

我正在做一些涉及使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

故事与诗 2024-11-07 02:50:04

我通常使用 Json.NET 从 JSON 文档中挑选值。语法非常简洁。如果引用NewtonSoft.Json.dll使用Newtonsoft.Json.Linq,则可以编写以下内容:

var job = JObject.Parse(jsonString);
if (job["data"]["expand"] == null)
{
  Console.WriteLine((string)job["data"]["url"]);
}
else
{
  Console.WriteLine((string)job["data"]["expand"][0]["long_url"]);
}

如果jsonString 是:

string jsonString = @"{""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 }}";

例程将显示http://rlm.cc/gtYUEd

如果 jsonString 为:

string jsonString = @"{""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""  }  ] } }";

例程将显示 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:

var job = JObject.Parse(jsonString);
if (job["data"]["expand"] == null)
{
  Console.WriteLine((string)job["data"]["url"]);
}
else
{
  Console.WriteLine((string)job["data"]["expand"][0]["long_url"]);
}

If jsonString is:

string jsonString = @"{""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 }}";

the routine will display http://rlm.cc/gtYUEd.

If jsonString is:

string jsonString = @"{""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""  }  ] } }";

the routine will display http://panel.aspnix.com/Default.aspx?pid={Removed}.

满地尘埃落定 2024-11-07 02:50:04

不确定我是否明白你的问题。如果您得到缩短结果或扩展结果,为什么不进行测试呢?由于它们不同,因此可以通过简单的“if ()”语句轻松完成:

if (results2.ContainsKey("expand")) {
    // handle the expand part
} else {
    // handle the shorten part

}

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:

if (results2.ContainsKey("expand")) {
    // handle the expand part
} else {
    // handle the shorten part

}
回梦 2024-11-07 02:50:04

假设提供者与其发送的表单一致,您是否需要拥有处理这两者的代码?应该直接单独处理每个。

如果您无法提前知道将返回哪种格式,您可以执行以下操作:

if (results2.ContainsKey("expand"))
{
    //Second example
}
else
{
    //First example
}

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:

if (results2.ContainsKey("expand"))
{
    //Second example
}
else
{
    //First example
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文