【问题标题】:Return json without binding it to object using web client使用 Web 客户端返回 json 而不将其绑定到对象
【发布时间】:2014-04-20 05:54:05
【问题描述】:

我想简单地从在线 API 服务 (iTunes) 返回 C# 中的 JsonResult。我想要做的只是以 JSON 格式获取数据并以相同的 JSON 格式返回该确切数据,以便我可以在 javascript 中使用它。

这是我所拥有的:

public JsonResult Index()
        {
            using (var client = new WebClient())
            {
                var json = client.DownloadString("https://itunes.apple.com/lookup?id=909253");

                return json;
            }

        }

我注意到我无法返回 json,因为它现在是一个字符串。我不想把它绑定到模型上!!!我只想准确地返回一个 JSON 对象。

【问题讨论】:

    标签: c# asp.net-mvc json api webclient


    【解决方案1】:

    更改方法签名以返回字符串而不是 JsonResult 对象……

    public string Index()
    {
        using (var client = new WebClient())
        {
            return client.DownloadString("https://itunes.apple.com/lookup?id=909253");
        }
    }
    

    【讨论】:

      【解决方案2】:

      已经给出的答案是可以在 javascript 中获取 json..javasript 会将此字符串视为您返回的 json 对象..

      但是,如果您必须从 c# 中的字符串获取 json 对象,请在此处检查接受的答案

      Parse JSON String to JSON Object in C#.NET

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-05-05
        • 2016-12-23
        • 2021-01-26
        • 2016-02-12
        • 2017-11-14
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多