I am dealing with a Json
object with an a priori partly unknown structure. I am not sure whether I can use the deserialising object method, because I cannot predict the exact structure length and naming I wish to target.
I simply know that the structure skeleton (looking at the Json
previewer) is going to be like:
[JSON]
[meta]
metaname1: metavalue1
...
metanameN: metavalueN
[target]
[0]
targetname1: targetvalue1
...
targetnameP: targetvalueP
The actual extracted structure is as follows:
{[meta, {
"limit": 100,
"offset": 0,
"count": 1,
"total": 1
}]}
{[target, [
{
"open": 173.99,
"high": 175.46,
"low": 172.675,
"last": 175.38,
"close": 173.66,
"volume": 607334.0,
"date": "2023-10-05T21:00:00+02:00",
"symbol": "AAPL",
"exchange": "IEXG"
}
]]}
I would like to skip the ‘meta’ node and focus on the ‘target’ node and push the series of keys and values (targetname1, targetvalue1),…, (targetnameP, targetvalueP) into a dictionary. I have loaded the Json
object using Newtonsoft.Json
library after reading the data from internet at a given ‘address’.
using (HttpClient client = new HttpClient())
{
Task<HttpResponseMessage> response = client.GetAsync(address);
using (HttpResponseMessage message = response.Result)
{
using (HttpContent content = message.Content)
{
string jsonData = content.ReadAsStringAsync().Result;
JObject jsonObj = JObject.Parse(jsonData);
}
}
}
I am not sure how to proceed next.
Addendum
Now I am trying this but I am still stuck.
class Program
{
static void Main(string[] args)
{
string jsonData = @"
{[meta, {
"limit": 100,
"offset": 0,
"count": 1,
"total": 1
}]}
{[target, [
{
"open": 173.99,
"high": 175.46,
"low": 172.675,
"last": 175.38,
"close": 173.66,
"volume": 607334.0,
"date": "2023-10-05T21:00:00+02:00",
"symbol": "AAPL",
"exchange": "IEXG"
}
]]}";
Dictionary<string, object> jsonDict = JsonConvert.DeserializeObject<Dictionary<string, object>>(jsonData);
object jsonSubData = jsonDict["target"];
}
}
I am failing to get ‘jsonSubData’ into another dictionary.
Is it an efficient solution?
I have come up with this. It seems to work. I am not sure it is the most efficient solution.
class Program
{
static void Main(string[] args)
{
string jsonData = @"Same structure as before";
JObject jsonObj = JObject.Parse(jsonData);
JObject jsonSubObj = jsonObj["data"].Values<JObject>().First();
Dictionary<string, string> jsonDict = new Dictionary<string, string>();
foreach (JProperty property in jsonSubObj.Properties())
jsonDict.Add(property.Name, property.Value.ToString());
}
}
2
Answers
I’m assuming the json root will always contain
meta
andtarget
properties. If so, you can do this:then, you just do this :
now you can access the
Data
property, and iterate over all its elements (objects).if you’re using
System.Text.Json
, then you just change the following :if your json root has more keys than the example above, and you only want
target
, then you could omit theMeta
property, then use theJsonSerializerSettings
and set theMissingMemberHandling
toMissingMemberHandling.Ignore
, this will ensure you only deserialize whatever you provide in theDataContainer
class.