C#将字符串转换为字典

卢卡斯

我从Bitly api获得以下响应字符串

{ "status_code": 200,
  "status_txt": "OK",
  "data":
    { "long_url": "http:\/\/amazon.de\/",
      "url": "http:\/\/amzn.to\/1mP2o58",
      "hash": "1mP2o58",
      "global_hash": "OjQAE",
      "new_hash": 0
    }
}

如何将该字符串转换为字典,以及如何访问键的值"url"(不使用\

约翰逊·沙林格

如果在项目中添加了Newtonsoft的Json之类的包,则可以将Json反序列化为匿名类型。然后,您可以从中获取网址。这可以通过Visual Studio中的NuGet获得,并支持异步或同步序列化/反序列化。

public string GetUrl(string bitlyResponse)
{
    var responseObject = new
    {
        data = new { url = string.Empty },
    };

    responseObject = JsonConvert.DeserializeAnonymousType(bitlyResponse, responseObject);
    return responseObject.data.url;
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章