-3

I get this Json response from sharepoint 2013 rest api :

{"odata.metadata":"https://killer33.sharepoint.com/sites/dev/_api/$metadata#SP.ListData.MessageListItems&$select=Title,Mesage,Description,Priority,ID","value":[{"odata.type":"SP.Data.MessageListItem","odata.id":"ed1c9e96-8dde-4498-b7e9-4db2bcb9e1ea","odata.etag":"\"1\"","odata.editLink":"Web/Lists(guid'92804422-bd9e-4d7b-baf0-5c2de0953b95')/Items(1)","Id":1,"Title":"ok","Mesage":"get","Description":"fd","Priority":"Medium","ID":1},{"odata.type":"SP.Data.MessageListItem","odata.id":"9afa3cd5-a6a1-40e7-a484-f608676096f3","odata.etag":"\"1\"","odata.editLink":"Web/Lists(guid'92804422-bd9e-4d7b-baf0-5c2de0953b95')/Items(2)","Id":2,"Title":"gf","Mesage":"fgf","Description":"gfgf","Priority":"High","ID":2},{"odata.type":"SP.Data.MessageListItem","odata.id":"df2c06fd-46b9-4a68-b2d5-563f64c8743d","odata.etag":"\"1\"","odata.editLink":"Web/Lists(guid'92804422-bd9e-4d7b-baf0-5c2de0953b95')/Items(3)","Id":3,"Title":"gfgf","Mesage":"fgf","Description":"fggf","Priority":"Medium","ID":3},{"odata.type":"SP.Data.MessageListItem","odata.id":"986dc67a-44d3-4c36-8e56-9c93f81107f2","odata.etag":"\"1\"","odata.editLink":"Web/Lists(guid'92804422-bd9e-4d7b-baf0-5c2de0953b95')/Items(4)","Id":4,"Title":"fgfg","Mesage":"gf","Description":"gff","Priority":"Medium","ID":4},{"odata.type":"SP.Data.MessageListItem","odata.id":"d33b78da-1493-4d2c-a7b6-8afaa0503506","odata.etag":"\"2\"","odata.editLink":"Web/Lists(guid'92804422-bd9e-4d7b-baf0-5c2de0953b95')/Items(5)","Id":5,"Title":"fgf","Mesage":null,"Description":null,"Priority":"Low","ID":5},{"odata.type":"SP.Data.MessageListItem","odata.id":"4e65919d-2ec4-480a-a803-a9fdba355c32","odata.etag":"\"1\"","odata.editLink":"Web/Lists(guid'92804422-bd9e-4d7b-baf0-5c2de0953b95')/Items(6)","Id":6,"Title":"gfg","Mesage":"dfd","Description":"fdf","Priority":"Medium","ID":6},{"odata.type":"SP.Data.MessageListItem","odata.id":"d9c50d4f-7fea-49fe-9389-1b8f547c1cb0","odata.etag":"\"1\"","odata.editLink":"Web/Lists(guid'92804422-bd9e-4d7b-baf0-5c2de0953b95')/Items(7)","Id":7,"Title":"dfdfd","Mesage":"ff","Description":"fgfg","Priority":"Medium","ID":7},{"odata.type":"SP.Data.MessageListItem","odata.id":"6d82e510-13b0-4a17-abbe-d19117063de6","odata.etag":"\"1\"","odata.editLink":"Web/Lists(guid'92804422-bd9e-4d7b-baf0-5c2de0953b95')/Items(8)","Id":8,"Title":"rt","Mesage":"ggf","Description":"fgd","Priority":"Low","ID":8}]}

I need to pick only id,Title,Description,Priority fields.
how can I do this ?

Draken
  • 3,134
  • 13
  • 34
  • 54
  • 1
    what have you tried so far? have you google for json parsing in .net already? – Peter Bons May 21 '17 at 10:48
  • you can read this here http://stackoverflow.com/questions/26820275/how-to-deserialize-odata-json – Rafał Developer May 21 '17 at 10:57
  • Step 1 - Go to your favourite search engine; Step 2 - Read a tutorial; Step 3 - Come back to SO when you have an *actual* problem, instead of just not being bothered to research how to come up with a solution to what you're doing – Geoff James May 21 '17 at 11:19

1 Answers1

2

There are many ways to parse JSON in c#, one of them is using Newtonsoft.JSON

You need to install it using Nuget Package.

Then you need to create model for your JSON.

public class Value
{
  [JsonProperty("odata.type")]
  public string odata_type { get; set; }
  [JsonProperty("odata.id")]
  public string odata_id { get; set; }
  [JsonProperty("odata.etag")]
  public string odata_etag { get; set; }
  [JsonProperty("odata.editLink")]
  public string odata_editLink { get; set; }
  public int Id { get; set; }
  public string Title { get; set; }
  public string Mesage { get; set; }
  public string Description { get; set; }
  public string Priority { get; set; }
  public int ID { get; set; }
}

public class Response
{
   [JsonProperty("odata.metadata")]
   public string odata_metadata { get; set; }
   public List<Value> value { get; set; }
}

Parsing logic

Response response = JsonConvert.DeserializeObject<Response>(json); //json is your json string

Hope it helps!!

Jayakrishnan
  • 4,232
  • 2
  • 23
  • 35
  • 2
    I am not criticising your answer, as I'm sure it answers the OP's question just fine. However, I think you need to take a more pragmatic approach when answering questions on SO. Where the OP clearly has not put any effort into researching what they want to do (there are a million and 1 other answers/tutorials on JSON and .NET), I think posting an answer anyway will just attract the wrong kind of attention to a bad question. – Geoff James May 21 '17 at 11:16
  • Hello james i tried to to convert json to c # class with the help of tool present in this site http://json2csharp.com/ it returned a class which is not help full my json contains odata i never worked on odata stuff i did my initial research for 5 hours and posted here – Shiva Prasad Pola May 23 '17 at 09:26