I know hot to Deserialize JSONs like { "key1": "value1", "key2": "value2"}
But now I am trying to use instagram API and it returns me a Json in this form
{
"access_token": "fb2e77d.47a0479900504cb3ab4a1f626d174d2d",
"user": {
"id": "1574083",
"username": "snoopdogg",
"full_name": "Snoop Dogg",
"profile_picture": "..."
}
}
Since it is not structured like an array of keys and values I am confused.
I need to assign those values to the following class of mine.
public class AccessTokenResult
{
public string AccessTokenString { get; set; }
public class UserWhoTook
{
public int ID { get; set; }
public string UserName { get; set; }
public string FullName { get; set; }
}
}
Checkout JsonConvert and questions like Using JsonConvert.DeserializeObject to deserialize Json to a C# POCO class
I reccomend you use http://json2csharp.com/ to format your Objects. Just paste in the json and it should spit out the classes for you.
For example, the json you posted generated.
public class User
{
public string id { get; set; }
public string username { get; set; }
public string full_name { get; set; }
public string profile_picture { get; set; }
}
public class RootObject
{
public string access_token { get; set; }
public User user { get; set; }
}
You can use third party libraries for JSON, for example Newton Json.net: https://json.codeplex.com/
Related
I want to serialize/deserialize the following JSON:
{
"result": {
"ID": 1,
"TITLE": "Example",
"ARRAY": [
{
"Item1": "Result1",
"Item2": "Result2"
}
]
}
}
I tried with the following class format, but no sucess yet... Can someone help me deserialize it?
public class myClass
{
public string ID { get; set; }
[JsonProperty("TITLE")]
public string Name { get; set; }
}
obs.: Using the namespace Newtonsoft.JSON
In your example class definition above, you have called the class myClass but you would have had to call it result because ID and TITLE are members of the result JSON in the given example. myClass would not resolve to anything.
I don't know why you'd want to have a property called Name that is mapped to TITLE, but ok, if you want to do that you can modify the solution after you get it working.
Still, we're not done yet. You also have a JSON member called ARRAY and you need to define a separate class for that.
And still there is an additional problem: the result JSON is nested inside an implicit base object, so we need to define that as well. Let's call it BaseResult.
public class ARRAY
{
public string Item1 { get; set; }
public string Item2 { get; set; }
}
public class Result
{
public int ID { get; set; }
public string TITLE { get; set; }
public List<ARRAY> ARRAY { get; set; }
}
public class BaseResult
{
public Result result { get; set; }
}
If you are using Visual Studio, you can copy your JSON and paste it in any *.cs file with Edit > Paste Special > Paste JSON as Classes. It will generate POCO objects representing your JSON, which in your case will be this:
public class Rootobject
{
public Result result { get; set; }
}
public class Result
{
public int ID { get; set; }
public string TITLE { get; set; }
public ARRAY[] ARRAY { get; set; }
}
public class ARRAY
{
public string Item1 { get; set; }
public string Item2 { get; set; }
}
Then, asuming that you have your JSON in a string variable named data, you can deserialize it as follows:
var result= JsonConvert.DeserializeObject<Rootobject>(data);
I want to map this JSON into a .NET class. How can I map this JSON data into a class? Please suggest how. Here is the json:
{"results": [
"43853",
"43855",
"43856",
"43857",
{
"questionType": 3,
"choiceAnswers": [123]
}
]}
The easiest solution is to use Visual Studio Edit > Paste Special > Paste Json As Classes.
But as your json is an array of different objects the .NET class will just be
public class JsonDto
{
public List<object> Results { get; set; }
}
A list of objects will be painful to work with so I recommend that you to use a typed model but then you need to specify you need to define the values, here's an example
{"results": [
{
"key1":"43853",
"key2":"43855",
"key3":"43856",
"key4":"43857",
"question": {
"questionType": 3,
"choiceAnswers": [123]
}
}
]};
public class JsonDto
{
public List<ResultDto> Results { get; set; }
}
public class ResultDto
{
public string Key1 { get; set; }
public string Key2 { get; set; }
public string Key3 { get; set; }
public string Key4 { get; set; }
public QuestionDto Question { get; set; }
}
public class QuestionDto
{
public int QuestionType { get; set; }
public List<int> ChoiceAnswers { get; set; }
}
You can use online converter to convert json data to c# models http://json2csharp.com For your json it would be something like this.
public class RootObject
{
public List<object> results { get; set; }
}
I want to parse a piece of JSON with Newtonsoft Json.NET
JSON:
{
"USER":{
"result_id":"0",
"result_description":"NET Connections",
"cmlog_username":[
"8118236834",
"8118236834",
"8118236834"
],
"caller_id":[
"14cc20f7b05f",
"14cc20f7b05f",
"14cc20f7b05f"
]
}
}
Class
public class USER
{
public string result_id;
public string result_description;
public string[] cmlog_username;
public string[] caller_id;
}//USER
I convert it with below code but all of property value is NULL
USER con = JsonConvert.DeserializeObject<USER>(msg);
Your deserialization class is incorrect. Putting your JSON into json2csharp.com produces:
public class USER
{
public string result_id { get; set; }
public string result_description { get; set; }
public List<string> cmlog_username { get; set; }
public List<string> caller_id { get; set; }
}
public class RootObject
{
public USER USER { get; set; }
}
So you would need to do:
User con = JsonConvert.DeserializeObject<RootObject>(msg);
Your JSON object isn't a USER, it's an object that contains a USER.
It's because the JSON object you are trying to parse into a user is an object that has a property of 'user' that is a user object. That probably didn't make much sense. You could change your json to be
{
"result_id":"0",
"result_description":"NET Connections",
"cmlog_username":[
"8118236834",
"8118236834",
"8118236834"
],
"caller_id":[
"14cc20f7b05f",
"14cc20f7b05f",
"14cc20f7b05f"
]
}
and it will work.
Try adding the get and set to your class
public class USER
{
public string result_id { get; set; }
public string result_description { get; set; }
public string[] cmlog_username { get; set; }
public string[] caller_id { get; set; }
}//USER
I just can't find a proper solution for this problem though i came across the same topic which was asked before.
Here is my sample Json which i am posting to a web api controller
{
"AppointmentId ":2079,
"manageaptarray":[
"VanId":6,
"Doctors":[
{"Id":1,"Name":Doc1},
{"Id":2,"Name":Doc2}
]
]}
Here is my c# class
public class ManageDoctorModel
{
public int Id { get; set; }
public int AppointmentId { get; set; }
public DateTime? AppointmentAt { get; set; }
public DateTime ScheduledAt { get; set; }
public int? ModifiedBy { get; set; }
public DateTime? ModifiedDateTime { get; set; }
public Nullable<bool> rCreate { get; set; }
public Nullable<bool> rUpdate { get; set; }
public Nullable<bool> rDelete { get; set; }
public Nullable<bool> rView { get; set; }
public jsonarray[] manageaptarray { get; set; }
}
public class jsonarray
{
public int VanId { get; set; }
public string VanName { get; set; }
public List<Doctorslist> Doctors { get; set; }
}
}
when i do so i get the error "cannot deserialize the current json array (e.g. 1 2 3 ) into type...."
I searched around stack & other forums most of the suggestion was to deserialize the json array.So here is what i did in my c# code
List<jsonarray> jsondata = JsonConvert.DeserializeObject<System.Collections.Generic.List<jsonarray>>(value.manageaptarray.ToString());
Note : value is the param where i get this json.
I tried some changes in my class files like doing idictionary<string,object> but that gives me the same error.
Since i am working with angularjs i tried the following
json.stringify(jsondata)
angular.tojson(jsondata)
json.parse(jsondata)
but no luck with that.Please help me out here.To what i have understood i think passing array within an array is the problem that is causing the trouble.So if anyone can help me how to figure this out it would be greatful so that i wont be wasting more time on this.
Thanks in advance and wish you all a very happy new year 2016 <3
Kind Note to mods : I know this is a repeative question but most of the questions aren't dealt with array of array in json.
The JSON that you show in your question, in not a valid JSON:
{
"AppointmentId ":2079,
"manageaptarray": [
"VanId": 6,
"Doctors": [
{
"Id":1,
"Name": Doc1
},
{
"Id":2,
"Name":Doc2
}
]
]
}
The manageaptarray is a invalid array syntax, also in JSON syntax, Doc1 and Doc2 isn't a valid value, probably they are a string, so they should be between double quotes.
You must fix the JSON first, then you can look my answer Deserialize JSON into Object C#, that show how to convert a valid JSON in a C# class using Visual Studio 2013/15.
A valid JSON should be like that:
{
"AppointmentId ":2079,
"manageaptarray": [
{
"VanId": 6,
"Doctors": [
{
"Id":1,
"Name": "Doc1"
},
{
"Id":2,
"Name": "Doc2"
}
]
}
]
}
And the C# class that match this, is:
public class Rootobject
{
public int AppointmentId { get; set; }
public Manageaptarray[] manageaptarray { get; set; }
}
public class Manageaptarray
{
public int VanId { get; set; }
public Doctor[] Doctors { get; set; }
}
public class Doctor
{
public int Id { get; set; }
public string Name { get; set; }
}
Then you can Deserialize this JSON into a class now using:
Rootobject jsondata = JsonConvert.DeserializeObject<Rootobject>(jsonString);
I'm creating a Steam APP ( For the Steam Platform ), and i need to deserialize a JSON file.
{
"response": {
"success": 1,
"current_time": 1401302092,
"raw_usd_value": 0.245,
"usd_currency": "metal",
"usd_currency_index": 5002,
"items": {
"A Brush with Death": {
"defindex": [
30186
],
"prices": {
"6": {
"Tradable": {
"Craftable": [
{
"currency": "metal",
"value": 4,
"last_update": 1398990171,
"difference": 0.17
}
]
}
}
}
},
...
I just need to get Defindex and value. Already deserialized some simple JSON files, but i think this one is more complex.
For those who wants to know, I am using the API from BackpackTF...
Use NewtonSoft.Json And then you can use it as follows to get the data out.
dynamic json = JsonConvert.DeserializeObject(<yourstring>);
string currency = json.response.usd_currency; // "metal"
In general, what you want to do is making sure you have valid JSON (use JSON LINT for that), then get a C# class definition with Json2CSharp, then you will do something like this:
MyClass myobject=JsonConvert.DeserializeObject<MyClass>(json);
(We're assuming MyClass is based on what you got from Json2CSharp)
Then you access the values you want via the traditional C# dot notation.
Use a nuget package caller Newtonsoft.Json.5.0.8. it is on the nuget repository.
This line of code will take your json as a string, and turn it into its root object.
RootObject obj = JsonConvert.DeserializeObject<RootObject>(jsonString);
The Json you provided is slightly flawed, but im guessing that the structure of c# objects you would be looking for would be close to this:
public class Craftable
{
public string currency { get; set; }
public int value { get; set; }
public int last_update { get; set; }
public double difference { get; set; }
}
public class Tradable
{
public List<Craftable> Craftable { get; set; }
}
public class Prices
{
public Tradable Tradable{ get; set; }
}
public class Items
{
public List<int> defindex { get; set; }
public Prices prices { get; set; }
}
public class Response
{
public int success { get; set; }
public int current_time { get; set; }
public double raw_usd_value { get; set; }
public string usd_currency { get; set; }
public int usd_currency_index { get; set; }
public Items items { get; set; }
}
public class RootObject
{
public Response response { get; set; }
}