从JSON响应中检索元素

时间:2017-11-07 19:23:08

标签: c# json asp.net-web-api json-deserialization

我正在尝试从我们正在构建的Web API中的其他服务的响应中获取一个字段。 JSON看起来像这样:

{
"d": {
    "results": [{
        "__metadata": {
            "id": "Web/Lists(guid'4ddc-41e2-bb44-0f92ad2c0b07')/Items(164)",
            "uri": "https://teams.ax.org/sites/js/project/_api/Web/Lists(guid'4ddc-41e2-bb44-0f92ad2c0b07')/Items(164)",
            "etag": "\"6\"",
            "type": "SP.Data.St_x0020_CdsListItem"
        },
        "Folder": {
            "__deferred": {
                "uri": "https://teams.ax.org/sites/js/project/_api/Web/Lists(guid'4ddc-41e2-bb44-0f92ad2c0b07')/Items(164)/Folder"
            }
        },
        "ParentList": {
            "__deferred": {
                "uri": "https://teams.ax.org/sites/js/project/_api/Web/Lists(guid'4ddc-41e2-bb44-0f92ad2c0b07')/Items(164)/ParentList"
            }
        },
        "PM_x0020_NameId": 220,
        "St_x0020_Name": "<div class=\"ExternalClassA14DB0FF86994403B827D91158CF34B0\">KO</div>",
    }]
}}

我创建了这些模型类:

 public class SharepointDTO
 {
    public class Metadata
    {
        [JsonProperty("id")]
        public string id { get; set; }

        [JsonProperty("uri")]
        public string uri { get; set; }

        [JsonProperty("etag")]
        public string etag { get; set; }

        [JsonProperty("type")]
        public string type { get; set; }
    }

    public class Deferred
    {
        [JsonProperty("uri")]
        public string uri { get; set; }
    }

    public class Folder
    {
        [JsonProperty("__deferred")]
        public Deferred __deferred { get; set; }
    }

    public class ParentList
    {
        [JsonProperty("__deferred")]
        public Deferred __deferred { get; set; }
    }

    public class Result
    {
        [JsonProperty("__metadata")]
        public Metadata __metadata { get; set; }

        [JsonProperty("Folder")]
        public Folder Folder { get; set; }

        [JsonProperty("ParentList")]
        public ParentList ParentList { get; set; }

        [JsonProperty("PM_x0020_NameId")]
        public int PM_x0020_NameId { get; set; }

        [JsonProperty("St_x0020_Name")]
        public string St_x0020_Name { get; set; }
    }

    public class D
    {
        [JsonProperty("results")]
        public IList<Result> results { get; set; }
    }

    public class RootObject
    {
        [JsonProperty("d")]
        public D d { get; set; }
    }
}

没有尝试从Web API调用其余服务,需要从响应中获取St_x0020_Name并存储在字符串中。

  SharepointDTO.RootObject retSharepointobj = await GetfromSharepoint(StNumber);
  string StName = retSharepointobj.d.results.St_x0020_Name.ToString();

enter image description here

我正在GetfromSharepoint方法中反序列化JSON,如

using (var client_sharePoint = new HttpClient(handler))
{
    var response = client_sharePoint.GetAsync(SP_URL).Result;

    var responsedata = await response.Content.ReadAsStringAsync();
    var returnObj = JsonConvert.DeserializeObject<SharepointDTO.RootObject>(responsedata);

    return returnObj;
}

但它引发了一个错误:

  

&#39; System.Collections.Generic.IList&#39;不包含&#39; St_x0020_Name&#39;的定义没有扩展方法&#39; St_x0020_Name&#39;接受类型为'System.Collections.Generic.IList&#39;的第一个参数。可以找到(你错过了使用指令或程序集引用吗?)

2 个答案:

答案 0 :(得分:1)

import numpy from scipy.io import loadmat def read_fig(filename): output = {} d = loadmat(filename, squeeze_me=True, struct_as_record=False) matfig = d['hgS_070000'] childs = matfig.children ax1 = [c for c in childs if c.type == 'axes'][0] for line in ax1.children: try: if line.type == 'graph2d.lineseries': x = line.properties.XData y = line.properties.YData leg = line.properties.DisplayName print leg output[leg] = numpy.column_stack((x, y)) except: print 'One children is ignored...' return output 是一个数组,所以你要像

一样循环
SELECT e.*
FROM (SELECT day, item_id, count(*) as cnt,
             MIN(day) OVER (PARTITION BY item_id) as minday      
      FROM events
      GROUP BY day, item_id
     ) e
WHERE day = minday;

或获取特定元素,例如第一个元素,如:

results

或者你可以添加一个额外的支票

foreach(var item in retSharepointobj.d.results){
   string StName = item.St_x0020_Name.ToString();
}

答案 1 :(得分:0)

不应该这一行:

string StName = retSharepointobj.d.results.St_x0020_Name.ToString();

是这样的吗?

string StName = retSharepointobj.d.results.First().St_x0020_Name.ToString();