0

I am trying to get a field from the response from the rest services in the Web API we are building. The JSON looks like this:

{
"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>",
    }]
}}

I created these model classes:

 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; }
    }
}

No trying to call the rest service from the Web API and need to get the St_x0020_Name from response and store in a string.

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

enter image description here

I am deserializing the JSON in the GetfromSharepoint method like

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;
}

But it throws an error:

'System.Collections.Generic.IList' does not contain a definition for 'St_x0020_Name' and no extension method 'St_x0020_Name' accepting a first argument of type 'System.Collections.Generic.IList' could be found (are you missing a using directive or an assembly reference?)

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
xyz
  • 531
  • 1
  • 10
  • 31

2 Answers2

1

results is an array, so you have either loop thru like

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

or get a specific element, for example a first element like:

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

or you can add an extra check like that

SharepointDTO.RootObject retSharepointobj = await GetfromSharepoint(StNumber);
if(retSharepointobj.d.results.length > 0){
    string StName = retSharepointobj.d.results[0].St_x0020_Name.ToString();
}
Pavel Kovalev
  • 7,521
  • 5
  • 45
  • 67
  • But I am getting the entire `
    KO
    ` in to the string how can I get only the `KO` which is in between the div tags.
    – xyz Nov 07 '17 at 20:34
0

Shouldn't this line:

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

Be like this?

string StName = retSharepointobj.d.results.First().St_x0020_Name.ToString();
FailedUnitTest
  • 1,637
  • 3
  • 20
  • 43
  • But I am getting the entire
    KO‌​div> in to the string how can I get only the KO which is in between the div tags.
    – xyz Nov 07 '17 at 20:35
  • That is a different problem now. You may want to look at something like this: https://stackoverflow.com/questions/18153998/how-do-i-remove-all-html-tags-from-a-string-without-knowing-which-tags-are-in-it – FailedUnitTest Nov 07 '17 at 20:44