Bunny83 / SimpleJSON

A simple JSON parser in C#
MIT License
735 stars 294 forks source link

How to get the Key of JsonNode? #56

Closed TonyChenn closed 1 year ago

TonyChenn commented 1 year ago

How to get the Key of JsonNode?

Bunny83 commented 1 year ago

A JSONNode does not have any connection to a potential parent. So only the parent may know which key may belong to which child. A JSONNode may be related to a certain key in a parent object, but could also be just at a particular index in a parent array. Even that array index association is just one way. When an element of the array is removed, the following indices would change. So there can't be a fix relationship between where in a parent a certain node may be referenced. It's literally just wrappers for a Dictionary (JSONObject) or List (JSONArray).

When you iterate through a parent with foreach, by default the enumerator enumerates over KeyValuePair<string, JSONNode>. Though there is an implicit conversion from KeyValuePair<string, JSONNode> to JSONNode, so it's possible to ignore the key and just iterate through the values / JSONNodes. Of course in that case you don't have the key at hand.

What exact issue do you have? Or what problem you want to solve?

TonyChenn commented 1 year ago

thanks for your reply, i have already solved the problem。use KeyValuePair<string, JSONNode> struct to get Key。