Parse certain data using JSON.Net - c#

I'm trying to parse a certain link from some JSON data I'm getting but I can't seem to do it? Here's some sample data (from PayPal API):
{"id":"PAY-3YA6562986829024GK2JH7UQ","intent":"sale","state":"created","payer":{"payment_method":"paypal"},"transactions":[{"amount":{"total":"12.00","currency":"USD"},"description":"creating a payment","related_resources":[]}],"create_time":"2016-01-10T15:59:14Z","links":[{"href":"https://api.sandbox.paypal.com/v1/payments/payment/PAY-3YA6562986829024GK2JH7UQ","rel":"self","method":"GET"},{"href":"https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token=EC-5CP140577W0453458","rel":"approval_url","method":"REDIRECT"},{"href":"https://api.sandbox.paypal.com/v1/payments/payment/PAY-3YA6562986829024GK2JH7UQ/execute","rel":"execute","method":"POST"}]}
So I've tried to do the following:
dynamic stuff = JsonConvert.DeserializeObject(createdPayment.ConvertToJson());
string paymentURL = stuff.href;
MessageBox.Show(paymentURL);
And I've also tried to use Newtonsoft.Json.Linqand populate stuff.links into a JAraay and then pull the link via an index, with no luck. This method that I'm using doesn't give me an error, it just returns a blank string for me?
Any ideas?

The problem is that your "href" links are within your "links" property, you can access them like this:
dynamic stuff = JsonConvert.DeserializeObject(json);
foreach (var item in stuff.links)
{
MessageBox.Show(item.href);
}
EDIT: added example of getting a list of links
dynamic stuff = JsonConvert.DeserializeObject(json);
var links = new List<string>();
foreach (var item in stuff.links)
{
links.Add((string)item.href);
}

You should create a class which hold the values you want from the JSON string.
The class will look something like this:
public class Payer
{
public string payment_method { get; set; }
}
public class Amount
{
public string total { get; set; }
public string currency { get; set; }
}
public class Transaction
{
public Amount amount { get; set; }
public string description { get; set; }
public List<object> related_resources { get; set; }
}
public class Link
{
public string href { get; set; }
public string rel { get; set; }
public string method { get; set; }
}
public class RootObject
{
public string id { get; set; }
public string intent { get; set; }
public string state { get; set; }
public Payer payer { get; set; }
public List<Transaction> transactions { get; set; }
public string create_time { get; set; }
public List<Link> links { get; set; }
}
Then you will be able to do something like this (using Newtonsoft):
var object = JsonConvert.DeserializeObject<RootObject>(jsonstring);
You will then be able to iterate over the List<Link> links object and get the href value from there.

If you need parse data then you can simply try this example console app. And read more at Newtonsoft api reference.
class Program
{
class HrefResult
{
public string Href { get; set; }
public string Rel { get; set; }
public string Method { get; set; }
}
static void Main(string[] args)
{
String createdPayment = #"{""id"":""PAY - 3YA6562986829024GK2JH7UQ"",""intent"":""sale"",""state"":""created"",""payer"":{""payment_method"":""paypal""},""transactions"":[{""amount"":{""total"":""12.00"",""currency"":""USD""},""description"":""creating a payment"",""related_resources"":[]}],""create_time"":""2016 - 01 - 10T15: 59:14Z"",""links"":[{""href"":""https://api.sandbox.paypal.com/v1/payments/payment/PAY-3YA6562986829024GK2JH7UQ"",""rel"":""self"",""method"":""GET""},{""href"":""https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token=EC-5CP140577W0453458"",""rel"":""approval_url"",""method"":""REDIRECT""},{""href"":""https://api.sandbox.paypal.com/v1/payments/payment/PAY-3YA6562986829024GK2JH7UQ/execute"",""rel"":""execute"",""method"":""POST""}]}";
JObject stuff = JObject.Parse(createdPayment);
IList<Newtonsoft.Json.Linq.JToken> results = stuff["links"].Children().ToList();
IList<HrefResult> hrefResults = new List<HrefResult>();
foreach (JToken result in results)
{
HrefResult hrefResult = Newtonsoft.Json.JsonConvert.DeserializeObject<HrefResult>(result.ToString());
hrefResults.Add(hrefResult);
}
foreach(var elem in hrefResults) Console.WriteLine("{0}", elem.Href);
}
}

Related

ASP.NET C# Deserialized Json Object

I have json like this
{
"reader_name":"FX9600EAF871",
"mac_address":"84:24:8D:FC:0E:AD",
"tag_reads":[
{
"epc":"E28068100000003C0A05E3B7",
"antennaPort":"1",
"peakRssi":"-31",
"seenCount":"2458",
"timeStamp":"14/02/2022 22:50:24:356",
"channelIndex":"5"
}
]
}
I try using this code
public class TagRead
{
public string epc { get; set; }
public string pc { get; set; }
public string antennaPort { get; set; }
public string peakRssi { get; set; }
public string seenCount { get; set; }
public string timeStamp { get; set; }
public string phase { get; set; }
public string channelIndex { get; set; }
public string isHeartBeat { get; set; }
}
public class Hdr
{
public string reader_name { get; set; }
public string mac_address { get; set; }
public List<TagRead> tag_reads { get; set; }
}
var deserialized = Newtonsoft.Json.JsonConvert.DeserializeObject<Hdr>(json);
When try to print reader name using
deserialized.reader_name
it gets result FX9600EAF87
but when print
deserialized.tag_reads
it get nothing?
my question is How to get epc & antennaport data?
thank you
Because deserialized.tag_reads is a collection instead of base type or string, you might get the result that you didn't want to get.
How to get epc & antennaport data?
you might try to use deserialized.tag_reads with foreach to iterator the collection then do your logic
var deserialized = Newtonsoft.Json.JsonConvert.DeserializeObject<Hdr>(json);
foreach(var item in deserialized.tag_reads){
//item.epc
//item.antennaPort
}
c# online

JsonConvert.DeserializeObject System.NullReferenceException

I am trying to download a dynamic JSON from a site to the var Vjson.
deserialize it and show some content to the console.
I don't know what I am doing wrong. I receive a NullReferenceException error however I am sure there is data.
{
var client = new WebClient();
var Vjson = client.DownloadString(Some URL);
dynamic Post = JsonConvert.DeserializeObject( Vjson);
foreach (var item in Post.Match) //loop through class //Post Match error System.NullReferenceException
{
Console.WriteLine("{0} {1} \n", item.ip_str, item.port);
}
The class is
class Post
{
public class Location
{
public string city { get; set; }
....
public string country_name { get; set; }
}
public class Options
{
}
public class Scodder
{
public string crawler { get; set; }
...
public Options options { get; set; }
}
public class AngularJS
{
public IList<string> categories { get; set; }
}
public class Components
{
public AngularJS AngularJS { get; set; }
}
public class Http
{
public int? robots_hash { get; set; }
....
public string waf { get; set; }
}
public class Match // Post.Match
{
...
public int port { get; set; } //---- data i try to extract
....
public string ip_str { get; set; } //---- data i try to extract
...
}
public IList<Match> matches { get; set; }
public int total { get; set; }
}
What am I doing wrong?
Try
foreach (var item in Post.matches)
instead of
foreach (var item in Post.Match)
Try
public List<Match> matches { get; set; }
instead of
public IList<Match> matches { get; set; }
Also
foreach (var item in Post.Match)
But you have no such collection, you have collection public IList<Match> matches { get; set; }
Try this instead
foreach (var item in Post.matches)
And why cant you use generic version of deserialization?
JsonConvert.DeserializeObject<Post>(Vjson)
Since the json string is not provided and so without any debugging
I can think of 2 changes
Add a parameterless public constructor in the Post class
class Post
{
public Post()
{
}
}
and secondly deserialize the json string like below
var x1 = JsonConvert.DeserializeObject<Post>(Vjson);

Convert Rest API JSON Response into C# object

I have a code REST API response which is json, and parsing to JObject and pulling a value from it. But i am getting the error when parsing to JObject.
Error: "Unexpected character encountered while parsing value: S. Path '', line 0, position 0."
Is there any other way to convert Json string to C# object.
I have the following code:
using Newtonsoft.Json;
using (HttpResponseMessage message = httpclient.GetAsync(folderIdURL).Result)
{
if(message.IsSuccessStatusCode)
{
var dataobjects = message.Content.ReadAsStringAsync();
//dataobjects = "{"id":"https://gbc-dev5.cloud.wc.com/DctmRest/repositories/dmgbsap_crt/","title":"DQL query results","author":[{"name":"EMC Documentum"}],"updated":"2019-05-02T15:19:52.508+00:00","page":1,"items-per-page":100,"links":[{"rel":"self","href":"https://gbc-dev5.cloud.wc.com/DctmRest/repositories/dmgbsap_crt/?dql=SELECT%20r_object_id%2cobject_name%20FROM%20dm_sysobject%20WHERE%20FOLDER%20(%27%2fgbc%2fUS%2fOSA-ATTACHMENT%2f2019%27)"}],"entries":[{"id":"https://gbc-dev5.cloud.wc.com/DctmRest/repositories/dmgbsap_crt/?dql=SELECT%20r_object_id%2cobject_name%20FROM%20dm_sysobject%20WHERE%20FOLDER%20(%27%2fgbc%2fUS%2fOSA-ATTACHMENT%2f2019%27)&index=0","title":"0b0111738011c114","updated":"2019-05-02T15:19:52.508+00:00","published":"2019-05-02T15:19:52.508+00:00","links":[{"rel":"edit","href":"https://gbc-dev5.cloud.wc.com/DctmRest/repositories/dmgbsap_crt/objects/0b0111738011c114"}],"content":{"json-root":"query-result","definition":"https://gbc-dev5.cloud.wc.com/DctmRest/repositori es/dmgbsap_crt/types/dm_sysobject","properties":{"r_object_id":"0b0111738011c114","object_name":"04"},"links":[{"rel":"self","href":"https://gbc-dev5.cloud.wc.com/DctmRest/repositories/dmgbsap_crt/objects/0b0111738011c114"}]}},{"id":"https://gbc-dev5.cloud.wc.com/DctmRest/repositories/dmgbsap_crt/?dql=SELECT%20r_object_id%2cobject_name%20FROM%20dm_sysobject%20WHERE%20FOLDER%20(%27%2fgbc%2fUS%2fOSA-ATTACHMENT%2f2019%27)&index=1","title":"0b0111738011c115","updated":"2019-05-02T15:19:52.509+00:00","published":"2019-05-02T15:19:52.509+00:00","links":[{"rel":"edit","href":"https://gbc-dev5.cloud.wc.com/DctmRest/repositories/dmgbsap_crt/objects/0b0111738011c115"}],"content":{"json-root":"query-result","definition":"https://gbc-dev5.cloud.wc.com/DctmRest/repositories/dmgbsap_crt/types/dm_sysobject","properties":{"r_object_id":"0b0111738011c115","object_name":"05"},"links":[{"rel":"self","href":"https://gbc-dev5.cloud.wc.com/DctmRest/repositories/dmgbsap_crt/objects/0b0111738011c115"}]}}]}"
JObject responseObj = JObject.Parse(dataobjects.ToString());
String id = (String)responseObj["entries" -->"content"-->"properties"-->"object_name"];
}
}
}
I am expecting the value from (String)responseObject["enteries"]["content"][" properties"]["object_name"]
JObjects are a pain. You could get a sample of the JSON response and paste it into a converter like json2csharp.com. It will generate a class for you which you can then use like so:
Generated Class:
public class MyClass
{
public string SomeProperty { get; set; }
public string AnotherProperty { get; set; }
}
Usage:
if (message.IsSuccessStatusCode)
{
var deserializedObject = JsonConvert.DeserializeObject<MyClass>(response.Content.ReadAsStringAsync().Result);
Console.WriteLine(deserializedObject.SomeProperty);
}
I would suggest to follow those steps:
You need to check that your json is actually a json, because an error says it is not. You can use online tools like this
If possible, avoid JObject and generate real classes. It is not that hard if you know the structure, and you can use another online tools
Modify your code to use classes
so you will have something like:
using System;
using Newtonsoft.Json;
namespace ConsoleApp11
{
class Program
{
public class Message
{
public Enteries enteries { get; set; }
}
public class Enteries
{
public Content content { get; set; }
}
public class Content
{
public Properties properties { get; set; }
}
public class Properties
{
public string object_name { get; set; }
}
static void Main(string[] args)
{
var input = "{\"enteries\":{\"content\":{ \"properties\":{ \"object_name\":\"your value string\"}}}}";
Message msg = JsonConvert.DeserializeObject<Message>(input);
Console.WriteLine(msg?.enteries?.content?.properties?.object_name ?? "no value");
Console.ReadKey();
}
}
}
I hope it helps 😊
Thank you so much for all the help and trips. Finally i am able to get the required value from JSON string.
Here is the Final code json2csharp.com
public class Author
{
public string name { get; set; }
}
public class Link
{
public string rel { get; set; }
public string href { get; set; }
}
public class Link2
{
public string rel { get; set; }
public string href { get; set; }
}
public class Properties
{
public string r_object_id { get; set; }
public string object_name { get; set; }
}
public class Link3
{
public string rel { get; set; }
public string href { get; set; }
}
public class Content
{
public string json_root { get; set; }
public string definition { get; set; }
public Properties properties { get; set; }
public List<Link3> links { get; set; }
}
public class Entry
{
public string id { get; set; }
public string title { get; set; }
public DateTime updated { get; set; }
public DateTime published { get; set; }
public List<Link2> links { get; set; }
public Content content { get; set; }
}
public class RootObject
{
public string id { get; set; }
public string title { get; set; }
public List<Author> author { get; set; }
public DateTime updated { get; set; }
public int page { get; set; }
public int items_per_page { get; set; }
public List<Link> links { get; set; }
public List<Entry> entries { get; set; }
}
Using Newtonsoft.Json
First get the list of entries from the responseObj. Then loop each entries and use LINQ to JSON to get values by property name or index.
You can use Item[Object] index on JObject/JArray and then cast the returned JValue to the type you want
JObject responseObj = JObject.Parse(dataobjects.ToString());
// get JSON result objects into a list
IList<JToken> entries = responseObj ["entries"].Children().ToList();
foreach(JToken entry in entries)
{
string object_name = (string) entry["content"]["properties"]["object_name"];
}

Deserialize a .json string returns: 'Object reference not set to an instance of an object'

I am trying to read a .json response. I have pasted the response here:
https://pastebin.com/0Zgg39si
Then I use the code below. When I run the code, I get the below error for:
"var deserializedTickers"
System.NullReferenceException: 'Object reference not set to an instance of an object.'
The code is the below. I am not sure what is causing this?
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
public void test()
{
//responseBody holds the .json response
String responseBody = "";
var deserializedTickers = JsonConvert.DeserializeObject<TickersRoot>(responseBody);
foreach (var ticker in deserializedTickers.Tickers)
{
var symbol2 = ticker.Value.Symbol;
}
}
public class TickersRoot { public Dictionary<string, Ticker> Tickers { get; set; } }
public class Ticker
{
public string Symbol { get; set; }
public long Timestamp { get; set; }
public DateTime Datetime { get; set; }
public double High { get; set; }
public double Low { get; set; }
public double Bid { get; set; }
public double Ask { get; set; }
public double Vwap { get; set; }
public double Open { get; set; }
public double Close { get; set; }
public double Last { get; set; }
public double BaseVolume { get; set; }
public double QuoteVolume { get; set; }
public Info Info { get; set; }
}
public class Info
{
public List<string> a { get; set; }
public List<string> b { get; set; }
public List<string> c { get; set; }
public List<string> v { get; set; }
public List<string> p { get; set; }
public List<int> t { get; set; }
public List<string> l { get; set; }
public List<string> h { get; set; }
public string o { get; set; }
}
Based on the response, your Info class should be something like this (set the datatype to match your needs):
public class Info
{
public string Buy { get; set; }
public string Sell { get; set; }
public string Open { get; set; }
public string Low { get; set; }
public string High { get; set; }
public string Last { get; set; }
public string Vol { get; set; }
}
as you don't have a property called "Tickers" on the json body, call the JsonConver.DeserializeObject method like this:
var deserializedTickers = JsonConvert.DeserializeObject<Dictionary<string, Ticker>>(responseBody);
then you can iterate the result as:
foreach (var ticker in deserializedTickers)
{
var symbol2 = ticker.Value.Symbol;
}
I had this error when one of my [Serializable] objects only had 1 constructor with a required arg. When de-serializing, the newtonsoft.json package could not create the entity from the data, since it had a required param in its constructor.
I solved it by removing the constructor and remembering to call a helper function when instantiating objects that are not loaded from a file/json.
You can either change the root json object to have a property named "tickers" that encapsulates the dictionary
{
"tickers":{
"BTC/AUD": {
...
},
...
}
}
Or deserialize the original json directly into a dictionary
var deserializedTickers = JsonConvert.DeserializeObject<Dictionary<string, Ticker>>(responseBody);
You should also change the Info class to match the json schema

Deserialise a Kraken JSON in C#

I used to be loading data from Cryptocompare's API.
This post got me what I wanted for Cryptocompare.
Parsing Cryptocompare API Json data in C#
However, I notice that this API is not bulletproof, it's not always returning an answer. So I want to use Kraken itself.
Now I am trying to deserialise the new JSON but this is not straightforward as Cryptocompare's JSON.
Here is an example JSON:
https://api.kraken.com/0/public/OHLC?pair=ETHUSD&interval=30
{"error":[],"result":{"XETHZUSD":[[1519236000,"825.94","846.00","825.94","845.00","835.62","858.29381033",708],[1519237800,"846.00","848.84","835.55","836.00","844.31","647.42747317",731],[1519239600,"836.00","841.09","830.76","834.89","835.13","1051.44905097",609],[1520530200,"706.24","710.43","701.90","704.59","707.70","1763.41692283",459]],"last":1520528400}}
I have not been able to deserialise this JSON.
Solved!!!
final solution:
var Results_Exchange_Kraken = JsonConvert.DeserializeObject<DataRoot_Kraken>(Content_Exchange_Kraken);
var price_data_kraken = Results_Exchange_Kraken.result.XETHZUSD;
public class Kraken_Result
{
public List<List<object>> XETHZUSD { get; set; }
public int last { get; set; }
}
public class DataRoot_Kraken
{
public List<object> error { get; set; }
public Kraken_Result result { get; set; }
}
Thank you! I'll bookmark the URL.
Use this Model to de-serialise your data
public class Result
{
public List<List<object>> XETHZUSD { get; set; }
public int last { get; set; }
}
public class RootObject
{
public List<object> error { get; set; }
public Result result { get; set; }
}
Model Generated by Model Generator
Then use NewtonSoft for c# to de-serialise your data
E.g.
RootObject tmp = JsonConvert.DeserializeObject<RootObject>("JSON String here");
final solution:
var Results_Exchange_Kraken = JsonConvert.DeserializeObject<DataRoot_Kraken>(Content_Exchange_Kraken);
var price_data_kraken = Results_Exchange_Kraken.result.XETHZUSD;
public class Kraken_Result
{
public List<List<object>> XETHZUSD { get; set; }
public int last { get; set; }
}
public class DataRoot_Kraken
{
public List<object> error { get; set; }
public Kraken_Result result { get; set; }
}

Categories