3

I am posting to API and get a response

HttpWebResponse response = (HttpWebResponse)request.GetResponse();          
var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
var jsonResponse = JsonConvert.SerializeObject(responseString , Formatting.Indented);

and this is the value of jsonResponse :

"{\"Id\":333,
  \"Name\":\"TestProduct\",
  \"ApplicationId\":\"9edcc30d-7852-4c95-a6b2-1bf370655965\",
  \"Features\":[{
         \"Id\":301,
         \"Name\":\"Sodalis Mobile Client2.0\",
         \"Code\":\"Beeware.Sodalis.Mobile\",
         \"ProductId\":0,
         \"Selected\":null}]
}"

how can I read each filed ?
like Name , ApplicationId , Features-Name and Features-Code.

I would appreciate any help.

Maksim Simkin
  • 9,561
  • 4
  • 36
  • 49
S.D.H.Z
  • 57
  • 8
  • This should help you http://stackoverflow.com/questions/1207731/how-can-i-deserialize-json-to-a-simple-dictionarystring-string-in-asp-net – Raskayu Dec 22 '16 at 08:16
  • @Raskayu i used the code in the link you provided , i got this error : `Unexpected character encountered while parsing value: [. Path 'Features', line 1, position 98. ` – S.D.H.Z Dec 22 '16 at 08:22
  • You want to `Deserialize` the `responseString`, not `Serialize`! – vojta Dec 22 '16 at 08:24
  • Hi @S.D.H.Z you can create a class with the ** ApplicationId , Features-Name and Features-Code ,...** properties and in the JsonConvert you can convert the json as that class ...if you need more explain say to me – Ali Sheikh Nezami Dec 22 '16 at 08:27
  • @AliSheikhNezami if you explain more I would appreciate . I am a rookie here – S.D.H.Z Dec 22 '16 at 08:31
  • @S.D.H.Z Jost folow my answer .... good luck – Ali Sheikh Nezami Dec 22 '16 at 09:07

3 Answers3

2

You have to create a class like

public class Feature
{
    [JsonProperty("Id")]
    public int Id { get; set; }

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

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

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

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

public class YourApp
{
    [JsonProperty("Id")]
    public int Id { get; set; }

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

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

    [JsonProperty("Features")]
    public IList<Feature> Features { get; set; }
}

then to Deserialize it just do

YourApp p = JsonConvert.DeserializeObject<YourApp>(json);
Console.WriteLine(p.Name + p.ApplicationId);

You can read all the features by using a foreach since now all the feautures are there in your object

foreach(Feature AllFeatures in p.Features)
{
    Console.WriteLine(AllFeatures.Name + AllFeatures.Code);
}
Mohit S
  • 13,723
  • 6
  • 34
  • 69
1

First of all, create classes which will hold your response:

public class MyResponse {
    public int Id {get; set;}
    public string Name {get; set;}
    public string ApplicationID {get; set;}
    public IList<MyFeature> Features {get; set;}
}

public class MyFeature {
    public int Id {get; set;}
    public string Name {get; set;}
    public int ProductId {get; set;}
    public string Selected {get; set;}

} 

Then you can use JsonConvert:

MyResponse response = JsonConvert.DeserializeObject<MyResponse>(responseString);
string name = response.Name;
vojta
  • 5,591
  • 2
  • 24
  • 64
1

Try this classes

public class MyResponse
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string ApplicationID { get; set; }
    public List<MyFeature> Features { get; set; }
}

public class MyFeature
{
    public int Id { get; set; }
    public string Name { get; set; }

    public string Code { get; set; }
    public int ProductId { get; set; }

    public bool? Selected { get; set; }

}

then you can read json by this

string _Json = @"{'Id':333,'Name':'TestProduct','ApplicationId':'9edcc30d-7852-4c95-a6b2-1bf370655965','Features':[{'Id':301,'Name':'Sodalis Mobile Client2.0','Code':'Beeware.Sodalis.Mobile','ProductId':0,'Selected':null}]}";
MyResponse M = JsonConvert.DeserializeObject<MyResponse>(_Json);