JSON.NET JProperty路径不一致

时间:2019-04-14 05:22:00

标签: json.net

我正在尝试序列化字典并为每个元素使用JProperty Path属性。为了简化这种情况,我的词典有2个元素,第一个元素的键是“ key1”,第二个元素的键是“ key1.extended”。 json看起来像这样

"Dictionary": { "key1": { "Text": "1", "Id": 0 }, "key1.extended": { "Text": "2", "Id": 1 } }

循环遍历JProperties时,Text属性的路径如下:

Dictionary.key1.Text

字典['key1.extended']。文本

显然,当对象被反序列化时,这很好用,但是当我要检查特定元素时,我很难将key1识别为元素。

任何想法如何以格式检索所有属性的路径

字典['key1.extended']。文本

编辑: 这是重现问题的代码

using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;



namespace ConsoleApp1 {
    class Program {


        private class SomeText {
            public string Text { get; set; }
        }

        private class parent {
            public Dictionary<string, SomeText> data { get; set; }
            public parent() {
                data = new Dictionary<string, SomeText>();
                data.Add("key1", new SomeText() { Text = "value 1" });
                data.Add("key1.extended", new SomeText() { Text = "value 2" });
            }
        }

        static void Main(string[] args) {

            parent p = new parent();

            string strJson = JsonConvert.SerializeObject(p, Newtonsoft.Json.Formatting.None,
                                            new JsonSerializerSettings {
                                                NullValueHandling = NullValueHandling.Ignore
                                            });


            JObject jo = JObject.Parse(strJson);
            foreach (JProperty property in FindTokensByName(jo, "Text")) {
                Console.WriteLine( $"{property.Path}:{property.Value}");
            }
            Console.ReadKey();
        }


        public static List<JToken> FindTokensByName(JToken containerToken, string name, bool findFIrstIndent = false) {
            List<JToken> matches = new List<JToken>();
            FindTokensByName(containerToken, name, matches);
            return matches;
        }

        private static void FindTokensByName(JToken containerToken, string name, List<JToken> matches, bool findFirstIndent = false) {
            if (containerToken.Type == JTokenType.Object) {
                foreach (JProperty child in containerToken.Children<JProperty>()) {
                    bool found = false;
                    if (child.Name == name) {
                        //matches.Add(child.Value);
                        matches.Add(child);
                        found = true;
                    }
                    if (!findFirstIndent || !found)
                        FindTokensByName(child.Value, name, matches);
                }
            }
            else if (containerToken.Type == JTokenType.Array) {
                foreach (JToken child in containerToken.Children()) {
                    FindTokensByName(child, name, matches);
                }
            }
        }
    }
}

            string strJson = JsonConvert.SerializeObject(p, Newtonsoft.Json.Formatting.None,
                                            new JsonSerializerSettings {
                                                NullValueHandling = NullValueHandling.Ignore
                                            });

返回

{
  "data": {
    "key1": { "Text": "value 1" },
    "key1.extended": { "Text": "value 2" }
  }
}

循环后,属性path:value的控制台输出

data.key1.Text:value 1
data['key1.extended'].Text:value 2

第一个路径是 data.key1.Text

第二条路径是 数据['key1.extended']。文本

0 个答案:

没有答案