How to create class structure for json response object - c#

i have a json response object but i unable to create a class structure for following json response object .
{
"PWSESSIONRS": [
{
"PWPROCESSRS": {
"PWHEADER": {
"APP_ID": "HSA",
"ORG_ID": "HSA",
"OUT_PROCESS_ID": "weconnect_validate",
"IN_PROCESS_ID": "weconnect_validate",
"LOGIN_ID": "TEST10800"
},
"PWDATA": {
"weconnect_validate": {
"Row": [
{
"success": "1"
}
]
}
}
}
}
]
}
Please suggest me some class structure. I have created following class structure but Json.Convert unable to deserialize it.
public class LoginSuccess
{
public List<PwProcessorSuccess> PWSESSIONRS { get; set; }
}
public class RowSuccess
{
public string success { get; set; }
}
public class WeconnectValidateSuccess
{
public List<RowSuccess> Row { get; set; }
}
public class PwDataSuccess
{
public WeconnectValidateSuccess weconnect_validate { get; set; }
}
public class PwHeaderSucess
{
public string LOGIN_ID { get; set; }
public string ORG_ID { get; set; }
public string APP_ID { get; set; }
public string IN_PROCESS_ID { get; set; }
public string OUT_PROCESS_ID { get; set; }
}
public class PwProcessorSuccess
{
public PwHeaderSucess PWHEADER { get; set; }
public PwDataSuccess PWDATA { get; set; }
}
Please help.

I made the classes for you from the beginning so that following code to help you a lot and your problem will be solved instantly.
You can use the below code:
public class MainKeepClass
{
public PWSESSIONR[] PWSESSIONRS { get; set; }
}
public class PWSESSIONR
{
public PWPROCESSRS PWPROCESSRS { get; set; }
}
public class PWPROCESSRS
{
public PWHEADER PWHEADER { get; set; }
public PWDATA PWDATA { get; set; }
}
public class PWHEADER
{
public string APP_ID { get; set; }
public string ORG_ID { get; set; }
public string OUT_PROCESS_ID { get; set; }
public string IN_PROCESS_ID { get; set; }
public string LOGIN_ID { get; set; }
}
public class PWDATA
{
public Weconnect_Validate weconnect_validate { get; set; }
}
public class Weconnect_Validate
{
public Row[] Row { get; set; }
}
public class Row
{
public string success { get; set; }
}

Related

Deserialize nested JSON, C# [duplicate]

This question already has an answer here:
Deserialize Nested JSON
(1 answer)
Closed 1 year ago.
I need to deserialize some JSON with this format:
{
"message": {
"header": {
"status_code": 200,
"execute_time": 0.29062294960022,
"available": 10000
},
"body": {
"track_list": [
{
"track": {
"track_id": 45085706,
"track_name": "Love Overdose (Deboa & Hannah Holland Remix)",
"primary_genres": {
"music_genre_list": [
{
"music_genre": {
"music_genre_name": "Dance"
}
}
]
}
}
}
]
}
}
}
I have these classes which I got from online generator, so I assume they are ok.
public class Header
{
public int status_code { get; set; }
public double execute_time { get; set; }
public int available { get; set; }
}
public class MusicGenre
{
public int music_genre_id { get; set; }
public int music_genre_parent_id { get; set; }
public string music_genre_name { get; set; }
public string music_genre_name_extended { get; set; }
public string music_genre_vanity { get; set; }
}
public class MusicGenreList
{
public MusicGenre music_genre { get; set; }
}
public class PrimaryGenres
{
public List<MusicGenreList> music_genre_list { get; set; }
}
public class Track
{
public int track_id { get; set; }
public string track_name { get; set; }
public List<object> track_name_translation_list { get; set; }
public int track_rating { get; set; }
public int commontrack_id { get; set; }
public int instrumental { get; set; }
public int #explicit { get; set; }
public int has_lyrics { get; set; }
public int has_subtitles { get; set; }
public int has_richsync { get; set; }
public int num_favourite { get; set; }
public int album_id { get; set; }
public string album_name { get; set; }
public int artist_id { get; set; }
public string artist_name { get; set; }
public string track_share_url { get; set; }
public string track_edit_url { get; set; }
public int restricted { get; set; }
public DateTime updated_time { get; set; }
public PrimaryGenres primary_genres { get; set; }
}
public class TrackList
{
public Track track { get; set; }
}
public class Body
{
public List<TrackList> TrackList { get; set; }
}
public class Message
{
public Header header { get; set; }
public Body body { get; set; }
}
public class Root
{
public Message message { get; set; }
}
I tried to deserialize the JSON with this code:
using (StreamReader r = new StreamReader(#"c:\users\xxxx\desktop\1.json"))
{
string json = r.ReadToEnd();
var tracks = JsonConvert.DeserializeObject<Track>(json);
}
but I got nothing. I'm new to this; made it with simpler JSON, but I can't figure out how to do it with this code. I want to print a list with just the song names.
If anyone can help me I would appreciate it!
There are a couple of problems here:
In your Body class, the TrackList property does not match the JSON. The corresponding property in the JSON is called track_list. The class properties must either exactly match the JSON (ignoring case) or else you need to use a [JsonProperty] attribute on the property to indicate what the JSON name will be. For example:
public class Body
{
[JsonProperty("track_list")]
public List<TrackList> TrackList { get; set; }
}
You are attempting to deserialize into the Track class, but you should be deserializing to Root since that represents the root of the JSON.
var root = JsonConvert.DeserializeObject<Root>(json);
Once you have deserialized to Root you can "drill down" to print out the tracks.
foreach (var item in root.message.body.TrackList)
{
Console.WriteLine(item.track.track_name);
}
Fiddle: https://dotnetfiddle.net/JnljGU

Deserialize Json Response with nested objects

I'm expecting this response from the API:
{
"EnterKey":"9876546789039876543567890",
"Id":1441462,
"Category":null,
"job":{
"Id":1020332,
"SortName":"test"
},
"Initiator":null,
"Source":{
"Id":1,
"Description":"data"
},
"BalanceNow":0.0,
"ready":false,
"Others":[
{
"Id":1255080,
"Amount":100.0,
"JobMethod":{
"Id":24,
"Description":"task",
"JobType":{
"Id":1,
"Description":"Other"
}
},
"Notes":null
}
],
"Messages":null,
"Products":[
{
"Tasks":{
"Id":2,
"Description":"Blah..."
},
"Join":null,
"TargetData":{
"PaymentId":1535026,
"WantedNotes":"Looks good",
"Name":"John"
},
"AdminDefinedFee":null,
"Product":"New"
}
]
}
I want to deserialize the above Json Response to get the WantedNotes from TargetData that is inside Products. I wanted it done with Json.NET so i tried doing:
public class datasummary
{
public List<TargetData> Products { get; set; }
}
public class TargetData
{
public string WantedNotes { get; set; }
}
var myresult = JsonConvert.DeserializeObject<datasummary>(jsonresponse);
That don't work. I don't know how that really is done. Can someone please show it's done correct?
Define the class structure like this:
public class Job
{
public int Id { get; set; }
public string SortName { get; set; }
}
public class Source
{
public int Id { get; set; }
public string Description { get; set; }
}
public class JobType
{
public int Id { get; set; }
public string Description { get; set; }
}
public class JobMethod
{
public int Id { get; set; }
public string Description { get; set; }
public JobType JobType { get; set; }
}
public class Others
{
public int Id { get; set; }
public double Amount { get; set; }
public JobMethod JobMethod { get; set; }
public object Notes { get; set; }
}
public class Tasks
{
public int Id { get; set; }
public string Description { get; set; }
}
public class TargetData
{
public int PaymentId { get; set; }
public string WantedNotes { get; set; }
public string Name { get; set; }
}
public class Product
{
public Tasks Tasks { get; set; }
public object Join { get; set; }
public TargetData TargetData { get; set; }
public object AdminDefinedFee { get; set; }
public string Product { get; set; }
}
public class DataDummary
{
public string EnterKey { get; set; }
public int Id { get; set; }
public object Category { get; set; }
public Job job { get; set; }
public object Initiator { get; set; }
public Source Source { get; set; }
public double BalanceNow { get; set; }
public bool ready { get; set; }
public List<Others> Others { get; set; }
public object Messages { get; set; }
public List<Product> Products { get; set; }
}
Then use:
var myresult = JsonConvert.DeserializeObject < DataSummary > (jsonresponse);
When working with json, you can copy all text from the file, add an new class then go to
Edit > Paste Especial > Paste JSON as Classes.
It will do all the work for you
Then you can use
var myresult = JsonConvert.DeserializeObject <DataSummary> (jsonresponse);

Json decoding not decoding into complex object

So I have a set of classes:
public class CallQueueRequest
{
public string ACK { get; set; }
public string ERROR { get; set; }
public Agent AGENT { get; set; }
public Skill SKILL { get; set; }
public string TIME { get; set; }
}
public class Agent
{
public string has_result { get; set; }
public List<Agents> agents { get; set; }
}
public class Agents
{
public string display_name { get; set; }
public string time_in_state { get; set; }
public string state { get; set; }
public string callstakentoday { get; set; }
public string avaya_skill_num { get; set; }
}
public class Skill
{
public string has_result { get; set; }
public string num_skills { get; set; }
public List<Skills> skills { get; set; }
}
public class Skills
{
public string display_name { get; set; }
public string avaya_skill_num { get; set; }
public string callsinqueue { get; set; }
public string callstoday { get; set; }
public string abantoday { get; set; }
public string lwt { get; set; }
public string ewt { get; set; }
public string servicelvl { get; set; }
public string avgspeedans { get; set; }
public string talktime { get; set; }
}
And I have this Json:
{
"ACK":"SUCCESS",
"ERROR":null,
"AGENT":{
"has_results":1,
"agents":[
{
"display_name":"John Doe",
"time_in_state":"378",
"state":"Acd",
"callstakentoday":null,
"avaya_skill_num":"81"
},
{
"display_name":"Jane Joe",
"time_in_state":"220",
"state":"Acd",
"callstakentoday":null,
"avaya_skill_num":"81"
}
]
},
"SKILL":{
"has_results":1,
"num_skills":1,
"skills":[
{
"display_name":"QueueName",
"avaya_skill_num":"81",
"callsinqueue":"1",
"callstoday":"29",
"abandtoday":"1",
"lwt":"74",
"ewt":"223",
"servicelvl":"86",
"avgspeedans":"35",
"talktime":"873"
}
]
},
"TIME":1355864270
}
I am using this code:
object qr = JsonConvert.DeserializeObject(jsonString);
This does not seem to be converting from Json to the complex class properly. Can someone assist me with this? I think its just a small mistake.
I was able to find some info on what I'm trying to do. If anyone finds this question, here is the answer

How to deserialize JSON to .NET object using JSON.NET?

I have a JSON object like the following
{
"data": [
{
"id": "18128270850211_49239570772655",
"from": {
"name": "Someone Unimportant",
"id": "57583427"
}
/* more stuff */
}
]
}
I want to parse it using JSON.NET,
FacebookResponse<FacebookPost> response = JsonConvert.DeserializeObject<FacebookResponse<FacebookPost>>(json);
internal class FacebookResponse<T> where T : class
{
public IList<T> Data { get; set; }
public FacebookResponsePaging Paging { get; set; }
}
public class FacebookPost
{
public string Id { get; set; }
[JsonProperty("to.data.id")]
public string FeedId { get; set; }
[JsonProperty("from.id")]
public string UserId { get; set; }
[JsonProperty("created_time")]
public DateTime CreatedTime { get; set; }
[JsonProperty("updated_time")]
public DateTime UpdatedTime { get; set; }
public string Type { get; set; } // TODO: Type enum??
public string Message { get; set; }
public string Link { get; set; }
public string Name { get; set; }
public string Caption { get; set; }
public string Description { get; set; }
}
Everything comes through except for the FeedId and the UserId properties. How should I be mapping these?
public class From
{
public string name { get; set; }
public string id { get; set; }
}
public class Datum
{
public string id { get; set; }
public From from { get; set; }
}
public class FacebookPost
{
public List<Datum> data { get; set; }
}
internal class FacebookResponse<T> where T : class
{
public IList<T> Data { get; set; }
public FacebookResponsePaging Paging { get; set; }
}
FacebookResponse<FacebookPost> response = JsonConvert.DeserializeObject<FacebookResponse<FacebookPost>>(json);
Try below code :)
Use this site to get the object for .net
Then you can use JSON.Net to deserialize: ex.JsonConvert.DeserializeObject(input) iirc

Need help Serializing/Deserializing this JSON

I have this simple JSON and I was making deserializing this JSON, so I can get the elements. This is the JSON I have :
{
"QuestionIDs": [
"QID1",
"QID3"
],
"QuestionDefinitions": {
"Question1": {
"DETag": "Q1",
"Config": {
"QDescription": "UseText"
},
"COrder": [
"1",
"2",
"3"
],
"Validation": {
"Settings": {
"ForceResponse": "OFF",
"ForceResponseType": "ON",
"Type": "None"
}
}
}
},
"NextButton": null,
"PreviousButton": false
}
This is the code I've written :
[DataContract]
public class RootObject
{
[DataMember]
public Dictionary<string, Dictionary<string, string>> QuestionDefinitions { get; set; }
[DataMember]
public List<string> QuestionIDs { get; set; }
}
QuestionIDs is working just fine. But, QuestionDefinitions isn't working. It says that it is an empty sequence. I'm not sure what's wrong.
I want to be able to access QuestionDefinitions.
I tried with Json.Net. Facing the same issue. I was able to get the simple elements that are out. But, couldn't get to access the QuestionDefinitions.
Any help would be appreciated.
EDIT :
I also tried implementing this class like this :
[DataContract]
public class QuestionDetails
{
[DataMember]
public string DETag { get; set; }
[DataMember]
[DataMember]
public List<Configuration> Config { get; set; }
[DataMember]
public List<string> ChoiceOrder { get; set; }
[DataMember]
public List<Validation> Validation { get; set; }
[DataMember]
public List<string> COrder { get; set; }
[DataMember]
public string NextButton { get; set; }
[DataMember]
public string PreviousButton { get; set; }
}
[DataContract]
public class RootObject
{
[DataMember]
public Dictionary<string, QuestionDetails> QuestionDefinitions { get; set; }
[DataMember]
public List<string> QuestionIDs { get; set; }
}
Your class strucutre against the JSON getting returned is not correct.
When pasted your JSON in json2csharp.com the following the POCO class which generated:
public class Config
{
public string QDescription { get; set; }
}
public class Settings
{
public string ForceResponse { get; set; }
public string ForceResponseType { get; set; }
public string Type { get; set; }
}
public class Validation
{
public Settings Settings { get; set; }
}
public class Question1
{
public string DETag { get; set; }
public Config Config { get; set; }
public List<string> COrder { get; set; }
public Validation Validation { get; set; }
}
public class QuestionDefinitions
{
public Question1 Question1 { get; set; }
}
public class RootObject
{
public List<string> QuestionIDs { get; set; }
public QuestionDefinitions QuestionDefinitions { get; set; }
public object NextButton { get; set; }
public bool PreviousButton { get; set; }
}
Now using the above class as representation of your JSON structure you should be able to deserialize it in to C# object.
The following line of code would do it using NewtonSoft library:
string json = "your json result";
RootObject result = JsonConvert.DeserializeObject<RootObject>(json);
You can even do it from within Visual Studio if you have 2013 or above doing like following as described in this post:
Hope it helps.
Changing a little the code in #EhsanSajjad's answer, maybe you could use something like:
public class Config
{
public string QDescription { get; set; }
}
public class Settings
{
public string ForceResponse { get; set; }
public string ForceResponseType { get; set; }
public string Type { get; set; }
}
public class Validation
{
public Settings Settings { get; set; }
}
public class Question
{
public string DETag { get; set; }
public Config Config { get; set; }
public List<string> COrder { get; set; }
public Validation Validation { get; set; }
}
public class RootObject
{
public List<string> QuestionIDs { get; set; }
public Dictionary<string, Question> QuestionDefinitions { get; set; }
public object NextButton { get; set; }
public bool PreviousButton { get; set; }
}
Try this model
public class RootObject
{
public List<string> QuestionIDs { get; set; }
public QuestionDefinitions QuestionDefinitions { get; set; }
public object NextButton { get; set; }
public bool PreviousButton { get; set; }
}
public class QuestionDefinitions
{
public Question1 Question1 { get; set; }
public List<string> COrder { get; set; }
public Validation Validation { get; set; }
}
public class Question1 {
public string DETag { get; set; }
public Config Config { get; set; }
}
public class Config {
public string QDescription { get; set; }
}
public class Validation {
public Settings Settings { get; set; }
}
public class Settings
{
public string ForceResponse { get; set; }
public string ForceResponseType { get; set; }
public string Type { get; set; }
}

Categories

Resources