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; }
}
Related
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
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; }
}
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);
I am trying to read a cell value from a simple google sheet
https://docs.google.com/spreadsheets/d/1opP1t_E9xfuLXBkhuyzo5j9k_xBNDx0XKb31JwLP1MM/edit?usp=sharing
then I published it and I get an API link that return JSON, check the following
https://spreadsheets.google.com/feeds/list/1opP1t_E9xfuLXBkhuyzo5j9k_xBNDx0XKb31JwLP1MM/1/public/values?alt=json
when I tried to generate C# classes from JSON using http://json2csharp.com/, I get
invalid_name and $ (which is not fine for csharp compiler)
public class Id
{
public string __invalid_name__$t { get; set; }
}
public class Updated
{
public DateTime __invalid_name__$t { get; set; }
}
public class Category
{
public string scheme { get; set; }
public string term { get; set; }
}
public class Title
{
public string type { get; set; }
public string __invalid_name__$t { get; set; }
}
public class Link
{
public string rel { get; set; }
public string type { get; set; }
public string href { get; set; }
}
public class Name
{
public string __invalid_name__$t { get; set; }
}
public class Email
{
public string __invalid_name__$t { get; set; }
}
public class Author
{
public Name name { get; set; }
public Email email { get; set; }
}
public class OpenSearchTotalResults
{
public string __invalid_name__$t { get; set; }
}
public class OpenSearchStartIndex
{
public string __invalid_name__$t { get; set; }
}
public class Id2
{
public string __invalid_name__$t { get; set; }
}
public class Updated2
{
public DateTime __invalid_name__$t { get; set; }
}
public class Category2
{
public string scheme { get; set; }
public string term { get; set; }
}
public class Title2
{
public string type { get; set; }
public string __invalid_name__$t { get; set; }
}
public class Content
{
public string type { get; set; }
public string __invalid_name__$t { get; set; }
}
public class Link2
{
public string rel { get; set; }
public string type { get; set; }
public string href { get; set; }
}
public class GsxName
{
public string __invalid_name__$t { get; set; }
}
public class GsxPhonenumber
{
public string __invalid_name__$t { get; set; }
}
public class Entry
{
public Id2 id { get; set; }
public Updated2 updated { get; set; }
public List<Category2> category { get; set; }
public Title2 title { get; set; }
public Content content { get; set; }
public List<Link2> link { get; set; }
public GsxName __invalid_name__gsx$name { get; set; }
public GsxPhonenumber __invalid_name__gsx$phonenumber { get; set; }
}
public class Feed
{
public string xmlns { get; set; }
public string __invalid_name__xmlns$openSearch { get; set; }
public string __invalid_name__xmlns$gsx { get; set; }
public Id id { get; set; }
public Updated updated { get; set; }
public List<Category> category { get; set; }
public Title title { get; set; }
public List<Link> link { get; set; }
public List<Author> author { get; set; }
public OpenSearchTotalResults __invalid_name__openSearch$totalResults { get; set; }
public OpenSearchStartIndex __invalid_name__openSearch$startIndex { get; set; }
public List<Entry> entry { get; set; }
}
public class RootObject
{
public string version { get; set; }
public string encoding { get; set; }
public Feed feed { get; set; }
}
I want to serialize and deserialize this class, also i want to remove $,what i need to do?
Obviously json2csharp converts each json object into a class and converts the key names into variable names literally. So whenever it finds a key name starting with $ it can't create a c# variable with this character and it precedes the variable name with a _invalid_name_. There is nothing wrong here.
You should tell why do you want to remove this invalid_name phrase from variable name? do you want to serialize and deserialize this class? If so you could use NewtonSoft Json library and define those fields with $ sign like this:
[JsonProperty(PropertyName = "$t")]
public string t { get; set; }
this will allow you to serialize/deserialize the json doc
same for gsx$name:
[JsonProperty(PropertyName = "gsx$name")]
public string gsxname { get; set; }
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