簡體   English   中英

使用C#從結果中解析JSON數據

[英]Parse JSON data from result with C#

當我嘗試從響應中獲取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);
    }
}

我在Google上找到的所有內容均無法正常運行。 我嘗試了這個,但是沒有用:

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

我正在使用活動廣告系列,因此可能不確定獲取json結果的另一種方法。

這是我想要的Json結果數據。 在此處輸入圖片說明

我知道api調用正在運行,因為我可以將Json數據寫入瀏覽器控制台。 return Content(response); 然后用jquery編寫。

這個問題已經回答了,但是我認為這很麻煩。

將respnse轉換為對象只是為了使其返回到相同狀態(JSON),這沒有意義。

您需要按原樣返回響應:

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

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

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

代替ActionResult,選擇JsonResult。 試試下面的代碼:

public JsonResult ActionName()  
{  
    your code goes here ....
    return Json(outputobj);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM