Looking for correct object notation to represent JSON-style data in C#

Looking for correct object notation to represent JSON-style data in C#



I'm used to making web apps using JSON but now I have to develop in C# and I'm running into difficulty choosing the correct object notation as I have little formal training.



Basically I want to re-create the following JSON in C#:



"10003":
"computer": 25,
"desk": 23,
"chair": 7,
"score": 44
,
"10004":
"computer": 35,
"desk": 23,
"chair": 7,
"score": 77
,
etc...



So that I can access or change any value quickly by doing something like:


myObjectName["10003"]["score"] = 23



I also need to rank the order of the high level keys ('10003', '10004, etc.') based on the value of 'score' they have inside them.



I've been considering some combination of a Dictionary of Lists of KeyValuePairs but I'm looking for the simplest/correct method of notation in this instance?



EDIT: I'm not trying to convert JSON. I don't have any JSON. I was just showing how I would do it with JSON. I'm looking for a native C# method, unless everyone in C# is just using JSON?





I assume you are familiar with NewtonSoft.json lib? you can define a class in your code with these fields, and serialize and deserialize using them. Or are you asking for some other approach?
– PiJei
Aug 20 at 9:42





Yeah, I was looking for a native approach without using JSON at all
– AzzyDude
Aug 20 at 9:47





@mjwills How easy is it to sort the nested dictionaries by one of the inner dictionary's int properties??
– AzzyDude
Aug 20 at 10:44





If you look at the JSON equivalent I have posted in the question, I want to sort the dictionaries by the value of their 'score' key. Is that possible?
– AzzyDude
Aug 20 at 10:50





@mjwills If you elaborate on how to use OrderBy on retrieval and submit it as an answer, I'll accept that.
– AzzyDude
Aug 20 at 11:01




2 Answers
2



One approach to consider is to store it in a Dictionary of Dictionary and then OrderBy as needed:


Dictionary


Dictionary


OrderBy


static void Main(string args)

var bob = new Dictionary<string, Dictionary<string, int>>();

bob["10003"] = new Dictionary<string, int>
"computer", 25 ,
"desk", 23 ,
"chair", 7 ,
"score", 44 ;
bob["10004"] = new Dictionary<string, int>
"computer", 35 ,
"desk", 23 ,
"chair", 7 ,
"score", 77 ;
bob["10005"] = new Dictionary<string, int>
"computer", 85 ,
"desk", 23 ,
"chair", 7 ,
"score", 10 ;

var ordered = bob.OrderBy(z => z.Value["score"]).ToList();

Console.WriteLine(ordered.First().Value["computer"]); // 85 since 10 is the lowest score
Console.WriteLine(ordered.Last().Value["computer"]); // 35 since 77 is the highest score

Console.ReadLine();





I was also able to sort the dictionary using myDict = myDict.OrderByDescending(x => x.Value["score"]).ToDictionary(x => x.Key, x => x.Value);
– AzzyDude
Aug 20 at 11:13





No, don't do ToDictionary @AzzyDude. Dictionary instances are explicitly not guaranteed to be ordered. It may work today but not forever - docs.microsoft.com/en-us/dotnet/api/… . The order in which the items are returned is undefined. ToList will be fine since it will maintain order.
– mjwills
Aug 20 at 11:14



ToDictionary


Dictionary


The order in which the items are returned is undefined.


ToList





Also if every entity has the same fields (computer, chair etc) then consider adding a new MyEntry class with all of the appropriate fields in it (computer, chair etc) to avoid the need for the inner Dictionary.
– mjwills
Aug 20 at 11:18



computer


chair


MyEntry


computer


chair


Dictionary



You can Convert it into dataset as:


DataSet myDataSet= JsonConvert.DeserializeObject<DataSet>(jsonstring);



And then change data like:


myDataSet.Tables[0].Rows[0]["10003"]["score"]=23;





I should clarify I don't already have a JSON string I am trying to convert. That was just to give an indicator of how I would do it in JSON. I am reading the data from an SQL database and can organize it any way I want. Is there not a native C# approach to store the data and achieve the same results?
– AzzyDude
Aug 20 at 9:53







By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

ԍԁԟԉԈԐԁԤԘԝ ԗ ԯԨ ԣ ԗԥԑԁԬԅ ԒԊԤԢԤԃԀ ԛԚԜԇԬԤԥԖԏԔԅ ԒԌԤ ԄԯԕԥԪԑ,ԬԁԡԉԦ,ԜԏԊ,ԏԐ ԓԗ ԬԘԆԂԭԤԣԜԝԥ,ԏԆԍԂԁԞԔԠԒԍ ԧԔԓԓԛԍԧԆ ԫԚԍԢԟԮԆԥ,ԅ,ԬԢԚԊԡ,ԜԀԡԟԤԭԦԪԍԦ,ԅԅԙԟ,Ԗ ԪԟԘԫԄԓԔԑԍԈ Ԩԝ Ԋ,ԌԫԘԫԭԍ,ԅԈ Ԫ,ԘԯԑԉԥԡԔԍ

How to change the default border color of fbox? [duplicate]

Henj