This is my first attempt at trying to parse a json response with json.net and I am totally lost. I have included a section of the parsed json below. What I would like to do do is loop through the backlinks array. I have tried implementing various samples from the newtonsoft documentation, but they don't seem to work and I think it is because my json doesn't match their samples and I don't have knowledge to make the necessary corrections. If someone could provide some C# code to get me started I would really appreciate it.
Thanks,
Chaos
{
"accounts": [
{
"10555": {
"sites": [
{
"12222": {
"pages_indexed_in_bing": {},
"download_time": null,
"backlinks": [
{
"anchor_text": "websites for insurance agents",
"source_url": "http://win-winbusinesses.com/insurance/how-to-building-an-effective-insurance-website/",
"found_on": "2015-07-15",
"page_authority": null,
"link_strength": 3,
"domain": "win-winbusinesses.com",
"domain_authority": 17
},
First of all, as stated before, the JSON you've provided is invalid.
I assume it should look like this:
{
"accounts": [
{
"10555": {
"sites": [
{
"12222": {
"pages_indexed_in_bing": {
},
"download_time": null,
"backlinks": [
{
"anchor_text": "websites for insurance agents",
"source_url": "http://win-winbusinesses.com/insurance/how-to-building-an-effective-insurance-website/",
"found_on": "2015-07-15",
"page_authority": null,
"link_strength": 3,
"domain": "win-winbusinesses.com",
"domain_authority": 17
}
]
}
}
]
}
}
]
}
And according to this JSON, you're classes should look like this if you want newtonsoft to succeed parsing:
public class PagesIndexedInBing
{
}
public class Backlink
{
public string anchor_text { get; set; }
public string source_url { get; set; }
public string found_on { get; set; }
public object page_authority { get; set; }
public int link_strength { get; set; }
public string domain { get; set; }
public int domain_authority { get; set; }
}
public class __invalid_type__12222
{
public PagesIndexedInBing pages_indexed_in_bing { get; set; }
public object download_time { get; set; }
public List<Backlink> backlinks { get; set; }
}
public class Site
{
public __invalid_type__12222 __invalid_name__12222 { get; set; }
}
public class __invalid_type__10555
{
public List<Site> sites { get; set; }
}
public class Account
{
public __invalid_type__10555 __invalid_name__10555 { get; set; }
}
public class RootObject
{
public List<Account> accounts { get; set; }
}
As you can see there might be problems since you using class/var names that are only numerical, so you probably should check this as well.
Related
I have the following JSON string
{
"data": [
{
"symbol": "1COV.GE",
"exposure": "0",
"makerExposure": "-2028",
"takerExposure": "2028",
"makerPnl": "447.6688",
"takerPnl": "-447.6688",
"makerPositions": [
{
"name": "IB_001",
"position": "-2028",
"vwap": "47.41",
"pnl": "447.6688"
}
],
"takerPositions": [
{
"name": "MT5_1",
"position": "2028",
"vwap": "47.41",
"pnl": "-447.6688"
}
]
},
{
"symbol": "A",
"exposure": "0",
"makerExposure": "-10",
"takerExposure": "10",
"makerPnl": "-4.6",
"takerPnl": "4.6",
"makerPositions": [
{
"name": "IB_002",
"position": "-10",
"vwap": "136.78",
"pnl": "-4.6"
}
],
"takerPositions": [
{
"name": "MT5_1",
"position": "10",
"vwap": "136.78",
"pnl": "4.6"
}
],
"total": 2
}
}
And my goal is to serialize it into a List of object from the NODE "Data":
I have the classes that map the data node fields:
public class Positions
{
public string name { get; set; }
public string position { get; set; }
public string vwap { get; set; }
public string pnl { get; set; }
}
public class ExPositions
{
public string symbol { get; set; }
public string exposure { get; set; }
public string makerExposure { get; set; }
public string takerExposure { get; set; }
public string makerPnl { get; set; }
public string takerPnl { get; set; }
public OZPositions makerPositions { get; set; }
public OZPositions takerPositions { get; set; }
}
Do you have any ideas how I can convert the node "data" to list of "ExPositions" objects, eg. List
I've did this but so far it throws an error
var positions = JsonSerializer.Deserialize<ExPositions>(json_string);
There is an error in your json - it's missing a closing ] for the array (I'll assume it's a typo).
The real problem is that you need a wrapper class to represent the data node of the json which should contain a list (or array) of ExPositions. The makerPositions and takerPositions should also become lists (or arrays) too. Add the following class and update the position properties of ExPositions:
public class Data
{
public List<ExPositions> data { get; set; }
}
// change positions to use a List too
public class ExPositions
{
...
public List<Positions> makerPositions { get; set; }
public List<Positions> takerPositions { get; set; }
}
Then you can deserialize using:
var result = JsonSerializer.Deserialize<Data>(json);
It's not clear where the ""total"": 2 property should be in your models (it's not clear in the json because of the issue I mentioned), you could add it to the Data class above (if it belongs there).
Online demo
Try with:
public class Positions
{
public string name { get; set; }
public string position { get; set; }
public string vwap { get; set; }
public string pnl { get; set; }
}
public class ExPositions
{
public string symbol { get; set; }
public string exposure { get; set; }
public string makerExposure { get; set; }
public string takerExposure { get; set; }
public string makerPnl { get; set; }
public string takerPnl { get; set; }
public Positions makerPositions { get; set; }
public Positions takerPositions { get; set; }
}
public class YourResult{
public ExPositions data { get; set; }
public int total { get; set; }
}
And then call:
var positions = JsonSerializer.Deserialize<YourResult>(json_string);
As haldo mentioned, there is a typo in your JSON. To quickly parse and validate your JSON data, you can use any online JSON parsers to validate your JSON data. I usually use the chrome extension JSON Viewer Pro.
Also, in the link that haldo provided to the .NET Fiddle for the demo, there is a trailing comma in JSON data which JSON deserializers might not ignore.
Here is the link to the edited demo that haldo provided.
Edited Demo
I'm trying to deserialize JSON without declaring every property in C#. Here is a cut-down extract of the JSON:
{
"resourceType": "export",
"type": "search",
"total": 50,
"timestamp": "2020-08-02T18:26:06.747+00:00",
"entry": [
{
"url": "test.com/123",
"resource": {
"resourceType": "Slot",
"id": [
"123"
],
"schedule": {
"reference": {
"value": "testvalue"
}
},
"status": "free",
"start": "2020-08-03T08:30+01:00",
"end": "2020-08-03T09:00+01:00"
}
}
]
}
I want to get the values out of entry → resource, id and start.
Any suggestions on the best way to do this?
I've made very good experiences with json2sharp. You can enter your JSON data there and it will generate the classes you need to deserialize the JSON data for you.
public class Reference
{
public string value { get; set; }
}
public class Schedule
{
public Reference reference { get; set; }
}
public class Resource
{
public string resourceType { get; set; }
public List<string> id { get; set; }
public Schedule schedule { get; set; }
public string status { get; set; }
public string start { get; set; }
public string end { get; set; }
}
public class Entry
{
public string url { get; set; }
public Resource resource { get; set; }
}
public class Root
{
public string resourceType { get; set; }
public string type { get; set; }
public int total { get; set; }
public DateTime timestamp { get; set; }
public List<Entry> entry { get; set; }
}
The next step is to choose a framework which will help you to deserialize. Something like Newtonsoft JSON.
Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse);
If you want to get the data without declaring classes, you can use Json.Net's LINQ-to-JSON API (JToken, JObject, etc.). You can use the SelectToken method with a JsonPath expression to get what you are looking for in a couple of lines. Note that .. is the recursive descent operator.
JObject obj = JObject.Parse(json);
List<string> ids = obj.SelectToken("..resource.id").ToObject<List<string>>();
DateTimeOffset start = obj.SelectToken("..resource.start").ToObject<DateTimeOffset>();
Working demo here: https://dotnetfiddle.net/jhBzl4
If it turns out there are actually multiple entries and you want to get the id and start values for all of them, you can use a query like this:
JObject obj = JObject.Parse(json);
var items = obj["entry"]
.Children<JObject>()
.Select(o => new
{
ids = o.SelectToken("resource.id").ToObject<List<string>>(),
start = o.SelectToken("resource.start").ToObject<DateTimeOffset>()
})
.ToList();
Demo: https://dotnetfiddle.net/Qe8NB7
I am not sure why you don't deserialize the lot (even if it's minimally populated) since you have to do the inner classes anyway.
Here is how you could bypass some of the classes (1) by digging into the JObjects
Given
public class Reference
{
public string value { get; set; }
}
public class Schedule
{
public Reference reference { get; set; }
}
public class Resource
{
public string resourceType { get; set; }
public List<string> id { get; set; }
public Schedule schedule { get; set; }
public string status { get; set; }
public string start { get; set; }
public string end { get; set; }
}
public class Entry
{
public string url { get; set; }
public Resource resource { get; set; }
}
You could call
var results = JObject.Parse(input)["entry"]
.Select(x => x.ToObject<Entry>());
I know people asked and already got some answers very similar question before like this, but still, I couldn't figure it out about mine. I have a JSON file contains a multidimensional object, like below:
{
"Common": {
"Required": "Required Entry ",
"Photos": "Photos",
"Videos": "Videos",
"Register": "Register"
},
"Forms": {
"Form": "Forms",
"Name": "Name",
"Phone": "Phone",
"Email": "Email",
"Message": "Message"
},
"Sections": {
"Home": {
"EventDateTime": "",
"MainTitle": "",
"SubTitle": ""
},
"About": {},
"Venue": {},
"Schedule": {},
"Speakers": {},
"Sponsors": {},
"Price": {},
"Contact": {}
}
}
I would like to deserialize it into my view model (LanguagesViewModel) like this:
[JsonObject(MemberSerialization = MemberSerialization.OptIn)]
public class LanguagesViewModel
{
public Common Common { get; set; }
public Buttons Buttons { get; set; }
public Forms Forms { get; set; }
public Navbar Navbar { get; set; }
public Sections Sections { get; set; }
}
public class Common
{
public string Required { get; set; }
public string Photos { get; set; }
public string Videos { get; set; }
public string Register { get; set; }
}
public class Forms
{
public string Form { get; set; }
public string Name { get; set; }
public string Phone { get; set; }
public string Email { get; set; }
public string Message { get; set; }
}
public class Sections
{
public Home Home { get; set; }
public About About { get; set; }
public Venue Venue { get; set; }
public Schedule Schedule { get; set; }
public Speakers Speakers { get; set; }
public Sponsors Sponsors { get; set; }
public Price Price { get; set; }
public Contact Contact { get; set; }
}
public class Home
{
public string EventDateTime { get; set; }
public string MainTitle { get; set; }
public string SubTitle { get; set; }
}
public class About
{
}
public class Venue
{
}
public class Schedule
{
}
public class Speakers
{
}
public class Sponsors
{
}
public class Price
{
}
public class Contact
{
}
}
Some of the snippet to do this:
using (StreamReader sr = new StreamReader(language_file_path))
{
string contents = sr.ReadToEnd();
items = JsonConvert.DeserializeObject<LanguagesViewModel>(contents);
}
Somehow, I only can get the first level of the objects, which is:
LanguagesViewModel{
Common:null,
Forms:null,
Sections:null
}
Not the second level, not the third level. Did I do something wrong or have I missed something? Very appreciated for any kind of help.
Thank you.
You can Use this static class
public static class JsonHelper
{
public static T ToObject<T>(this string content)
{
var obj = JObject.Parse(content).GetValue(typeof(T).Name);
if (obj == null)
throw new NullReferenceException();
else
return obj.ToObject<T>();
//This ToObject here is default method written in object
}
}
Usage
var mymodel= json.ToObject<Forms>();
Or create a JSON object and read it with magic strings.
//Creating your JSON object
JObject content = JObject.Parse(sr.ReadToEnd()//or your json);
//content["your object name"] let you access to you object
var common =(Common)content["Common"];
in multidimensional objects, you can access them like this.
//content["level1"]["level2"]["level3"] & ...
var sections= (Home)content["Sections"]["Home"];
Also this way may work but i prefer the way with magic strings.
dynamic jsonObject = new JObject.Parse(sr.ReadToEnd());
var common = jsonObject.Common;
You can find more in this link
I hope this Helps!
I am receiving the json response of below format from an api. I am trying to deserialze it with custom class.
{
"TraceEvent": {
"Attributes": {
"Commodity": "APPLES",
"Variety": "Green"
},
"Codes": [{
"devicename": "",
"code": "901491877572115",
"timestamp": "2018-02-15T19:33:29.4418926+05:30"
}, {
"devicename": "",
"code": "6657287134488755",
"timestamp": "2018-02-15T19:33:29.4418926+05:30"
}
]
}
}
Below is my custom class used for deserialize
public class EventContainer
{
[JsonProperty("TraceEvent")]
public TraceEvent TraceEvent { get; set; }
}
public class TraceEvent
{
[JsonProperty("attributes")]
public TraceAttributes Attributes { get; set; }
[JsonProperty("codes")]
public TraceCodes Codes { get; set; }
}
public class TraceAttributes
{
[JsonProperty("commodity")]
public string Commodity { get; set; }
[JsonProperty("variety")]
public string Variety { get; set; }
}
public class TraceCodes
{
public TraceCodes()
{
Codes = new List<TraceCode>();
}
[JsonProperty("Codes")]
public List<TraceCode> Codes { get; set; }
}
public class TraceCode
{
[JsonProperty("devicename")]
public string DeviceName { get; set; }
[JsonProperty("code")]
public string Code { get; set; }
[JsonProperty("timestamp")]
public DateTime Timestamp { get; set; }
}
In the receiver side, i am getting null for the Codes. Plesae refer my debug screen in api receiver code,
Can any one tell me how to rewrite my custom class to deserialize the Codes list from JSON api
Thanks for the help.
Change the class structure. The Codes should be in TraceEvent class not in its own class
public class TraceEvent
{
[JsonProperty("attributes")]
public TraceAttributes Attributes { get; set; }
[JsonProperty("Codes")]
public List<TraceCode> Codes { get; set; }
}
Remove below class
public class TraceCodes
{
public TraceCodes()
{
Codes = new List<TraceCode>();
}
[JsonProperty("Codes")]
public List<TraceCode> Codes { get; set; }
}
TraceEvent has a property
public TraceCodes Codes { get; set; }
And TraceCodes is another object with a list of codes:
public List<TraceCode> Codes { get; set; }
This would mean there would have to be a structure like this:
{
"TraceEvent": {
"Codes": {
"Codes": [
{ … },
{ … },
}
}
}
}
So the "Codes" part is double. Instead, you need to modify your TraceEvent to have that list directly:
public class TraceEvent
{
[JsonProperty("attributes")]
public TraceAttributes Attributes { get; set; }
[JsonProperty("Codes")]
public List<TraceCode> Codes { get; set; }
}
Btw. that should have actually resulted in a JsonSerializationException, so you should check whether that gets swallowed somewhere.
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; }
}