Can someone help me with the following:
I have the following JSON that is composed of several arrays of AA, BB, CC objects and each of these has its corresponding KEY "ts" and VALUE "value" objects.
{
"AA": [
{
"ts": 1636862399574,
"value": "2021-11-14 00:57:25.049983"
},
{
"ts": 1636862398995,
"value": "2021-11-14 00:57:24.049979"
}
],
"BB": [
{
"ts": 1636862399574,
"value": "16183.8"
},
{
"ts": 1636862398995,
"value": "16183.8"
}
],
"CC": [
{
"ts": 1636862399574,
"value": "0.0"
},
{
"ts": 1636862398995,
"value": "0.0"
}
]
}
My code snippet in C #:
List<List<DateTime>> ArrayAA;
List<List<double>> ArrayBB;
List<List<double>> ArrayCC;
for (int i = 0; i < devices.Count; i )
{
var data = JsonConvert.DeserializeObject(json);
if (data != null)
{
// Then deserialize your json to Dictionary<string, List<MyObject>>
var myDictionary = JsonConvert.DeserializeObject<Dictionary<string, List<MyObject>>>(JSONdata);
foreach (KeyValuePair<string, List<MyObject>> entry in myDictionary)
{
foreach (var obj in entry.Value)
{
Console.WriteLine($"{entry.Key} -> ts: {obj.TS}, value: {obj.Value}");
ArrayAA[i].Add(obj.value);
ArrayBB[i].Add(obj.value);
ArrayCC[i].Add(obj.value);
}
}
}
}
My code is not working, I need to read all the values at once for each AA, BB, CC.
It's possible?
CodePudding user response:
There are several ways to do this, one of which is to use the JObject
class
JObject data = JObject.Parse(json);
if (data != null)
{
var itemsA = data["AA"].Select(i => i["value"]);
foreach(var item in itemsA)
{
Console.WriteLine("Value: " item);
}
//and BB and CC.....................................
}
OR
JObject data = JObject.Parse(json);
Console.WriteLine("Value: " data["AA"][0]["value"]);
Console.WriteLine("Value: " data["AA"][1]["value"]);
//.......................................
Console.WriteLine("Value: " data["BB"][0]["value"]);
You can either use dynamic data type or pack in specific classes.
CodePudding user response:
You should first deserialize your object to a dictionary. This way you can iterate over each of the keys you have. Once you have the dictionary... you can print the values however you like.
this is an example that shows you how to access the values of each of the keys, "AA", "BB", "CC" etc.
// Build your object class first.
public class MyObject
{
[JsonProperty("ts")]
public long TS { get; set; }
[JsonProperty("value")]
public string Value { get; set; }
}
// Then deserialize your json to Dictionary<string, List<MyObject>>
var myDictionary = JsonConvert.DeserializeObject<Dictionary<string, List<MyObject>>>(jsonText);
foreach (KeyValuePair<string, List<MyObject>> entry in myDictionary)
{
foreach (var obj in entry.Value)
{
Console.WriteLine($"{entry.Key} -> ts: {obj.TS}, value: {obj.Value}");
}
}
List<DateTime> arrayAA = myDictionary["AA"].Select(x => DateTime.Parse(x.Value)).ToList();
List<double> arrayBB = myDictionary["BB"].Select(x => double.Parse(x.Value)).ToList();
List<double> arrayCC = myDictionary["CC"].Select(x => double.Parse(x.Value)).ToList();
// Prints the following output
AA -> ts: 1636862399574, value: 2021-11-14 00:57:25.049983
AA -> ts: 1636862398995, value: 2021-11-14 00:57:24.049979
BB -> ts: 1636862399574, value: 16183.8
BB -> ts: 1636862398995, value: 16183.8
CC -> ts: 1636862399574, value: 0.0
CC -> ts: 1636862398995, value: 0.0
UPDATE - How to add items? if you want to add items to the data... you will need to know which Key you want to add the item to.
string key = "AA";
if (myDictionary.ContainsKey(key))
{
myDictionary[key].Add(new MyObject() { TS = 651651651651, Value = "abc" });
}
// or get only the values and save them in separate arrays? use LINQ
string[] arrayAA = myDictionary["AA"].Select(x => x.Value).ToArray();
string[] arrayBB = myDictionary["BB"].Select(x => x.Value).ToArray();
CodePudding user response:
Another way is to create class based on the json string and the deserialize to that class
using Newtonsoft.Json;
public class MyRootClass
{
public List<AA> AA { get; set; }
public List<AA> BB { get; set; }
public List<AA> CC { get; set; }
}
public class AA
{
public long ts { get; set; }
public string value { get; set; }
}
public class BB
{
public long ts { get; set; }
public string value { get; set; }
}
public class CC
{
public long ts { get; set; }
public string value { get; set; }
}
string json = "yourJson";
var myObject = JsonConvert.DeserializeObject<MyRootClass>(json);
and to read it. loop over each list in the myobject
foreach (var item in myObject.AA)
{
Console.WriteLine(item.value);
Console.WriteLine(item.ts);
}