【问题标题】:C# - Parsing response content results from Zoho Creator Rest APIC# - 解析来自 Zoho Creator Rest API 的响应内容结果
【发布时间】:2020-08-14 00:34:15
【问题描述】:

解析 Zoho Creator Rest API 返回时遇到问题。样本返回结果:

{{"formname":["RestAPI",{"operation":["add",{"values":{"Name":"Gary","Basic":"10000","Hobbies": ["Reading","Writing"],"DOB":"12-Jun-1980","Address":"USA","ID":89597000010897007},"status":"Success"}]}]}

使用下面的代码返回一个 JObject 以提取属性:

/* send post request here */
HttpContent _content = null;
var response = this.client.PostAsync(this.PostUrl,_content).Result;  

/* parse request response here - need to record all data from return response */
string responseString = response.Content.ReadAsStringAsync().Result;
JObject result = JObject.Parse(responseString);

由于某种原因,我无法访问“结果”对象的任何属性。我尝试过使用索引和 JToken 方法,但都没有返回值。

【问题讨论】:

    标签: c# rest api zoho


    【解决方案1】:

    丑陋但有效 - 不幸的是它只适用于特定的回报,可能有一个使用类似方法的动态解决方案:

    /* parse request response here - need to record all data from return response */
    string responseString = response.Content.ReadAsStringAsync().Result;
    JObject result = JObject.Parse(responseString);
    var _formname = result["formname"] as JArray;
    var step1 = _formname[1];
    string operation = step1["operation"][0].ToString();
    var values = step1["operation"][1]["values"];
    var status = step1["operation"][1]["status"].ToString();
    

    【讨论】:

      【解决方案2】:

      我建议尝试 JsonConvert.DeserializeObject(导入 newtonsoft.json 包)并在您的 responseString 上使用它。您将需要创建一个与您收到的 JSON 的结构相匹配的 c# 类,以使 DeserializeObject 方法工作。如果您只想使用 .(whatever property) 从响应中获取信息,我建议尝试 Json.Decode(responseString);

      【讨论】:

      猜你喜欢
      • 2018-05-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多