c# - 使用 C# 解析结果中的 JSON 数据

标签 c# json parsing

当我尝试从响应中获取 Json 数据时,它不起作用。 我是 Json 新手,所以我不知道如何解决这个问题。

这是我的 atm 代码:​​

//process ajax and make API call to active campaign
    [HttpPost]
    public ActionResult ProcessPreferences(string categoryTag, string userEmailAddr)
    { 
        string applyToTag = categoryTag;
        string emailAddr = userEmailAddr;

        /*
         *   Active Campign API Integration
         */
        // By default, this sample code is designed to get the result from your ActiveCampaign installation and print out the result
        var acs = new Acs("api_key_removed", "api_url_removed");

        Dictionary<string, string> getParameters = new Dictionary<string, string>();
        getParameters.Add("api_action", "contact_view_email");
        getParameters.Add("api_output", "json");
        getParameters.Add("email", emailAddr);

        var response = acs.SendRequest("GET", getParameters);
        var obj = System.Web.Helpers.Json.Decode(response);

        return Content(obj.tags);
    }
}

我在谷歌上找到的所有东西都不起作用。 我尝试了这个,但它不起作用:

var obj = System.Web.Helpers.Json.Decode(response);
return Content(obj.tags);

我正在处理活跃的事件,因此可能是获取 json 结果的不同方法,不确定。

这是我想要从 Json 结果中得到的数据。 enter image description here

我知道 api 调用正在工作,因为我可以将 Json 数据写入浏览器控制台。 return Content(response);然后用jquery编写。

最佳答案

这个问题已经有了答案,但我觉得有点麻烦。

仅仅为了将响应返回到相同的状态(JSON)而将响应转换为对象是没有意义的。

您需要返回响应:

[HttpPost]
public ActionResult ProcessPreferences(string categoryTag, string userEmailAddr)
{
    ...

    var response = acs.SendRequest("GET", getParameters);

    return Content(response, "application/json");
}

关于c# - 使用 C# 解析结果中的 JSON 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44753924/

相关文章:

python - pydantic 模型动态字段数据类型

c++ - 如何在运行时计算 C++ 表达式类型?

javascript - 我如何解析从模型传输到 JavaScript 的日期值?

c# - 之后使用该属性的依赖属性?

c# - 如何在查询中执行查询并将其设置为主查询的条件

javascript - 访问json中的数据未获取 key

php - Laravel 4 JSON 输出格式

java - 在没有任何数组的情况下在Android中解析JSON数组

C# TabControl Selected 事件似乎不起作用

c# - 随机生成数字 1 的次数超过 90%