如何获取c#中解析器动态生成的.json文件的键和值

时间:2023-02-09 21:14:51

We have following scenario,

我们有以下的情况下,

We need to parse resumes of candidates and i am using below parser to parse resume in Json format, right.

我们需要解析候选人的简历,我正在使用下面的解析器来解析Json格式的简历。

https://github.com/antonydeepak/ResumeParser

https://github.com/antonydeepak/ResumeParser

and Json files which i get are in valid format as i checked them in online jsonviewer, but it is cleared that each resume is in different format. so each time parser introduced new pair of Keys ,

我在jsonviewer中查看的Json文件都是有效的格式,但是可以确定的是,每个简历都是不同的格式。每次解析器引入新的一对键时,

Example 1.

例1。

如何获取c#中解析器动态生成的.json文件的键和值

Example 2.

例2。

如何获取c#中解析器动态生成的.json文件的键和值

Above two formats are of two different resumes and so on...

以上两种格式是两种不同的简历,等等。

so now i need to iterate through every key and value that are dynamically generated.

现在我需要遍历动态生成的每个键和值。

as for as i did to get JObject and JArray at level 0, now i need to iterate through each JObject and JArray to get its values.

就像我在0级获得JObject和JArray一样,现在我需要遍历每个JObject和JArray来获得它的值。

I used Json.net to get them

我使用Json.net来获取它们。

string text = System.IO.File.ReadAllText(@"C:\abc.json");
var d = JsonConvert.DeserializeObject<Dictionary<string, dynamic>>(text);

and it showed me data as

它显示的数据是

如何获取c#中解析器动态生成的.json文件的键和值

abc.json has JObjects and JArray, so now i need to iterate through each and every line and need to get every key and value from parsed json file and load it to datatable and tried it using google , but it missed some of keys and values.

美国广播公司(abc)。json有JObjects和JArray,所以现在我需要遍历每一行,需要从解析的json文件中获取每一个键和值,并将其加载到datatable中,并使用谷歌进行尝试,但它漏掉了一些键和值。

System.Data.DataTable dt = new System.Data.DataTable();
            dt.Columns.Add("Key", typeof(string));
            dt.Columns.Add("Value", typeof(string));
            string text = System.IO.File.ReadAllText(@"C:\antony_thomas.json");
var d = JsonConvert.DeserializeObject<Dictionary<string, dynamic>>(text);

            foreach (var item in d)
            {
                var key = "";
                var val = item.Value;

                if (val is JObject)
                {
                    dynamic dynObj = JsonConvert.DeserializeObject(Convert.ToString(val));
                    foreach (var ite in dynObj)
                    {
                        DataRow row = dt.NewRow();
                        string jsonvalue = Convert.ToString(ite).Replace("[", "").Replace("]", "").Replace("{", "").Replace("}", "").Replace("\"", "");
                        string jkey = jsonvalue.Split(':')[0];
                        string jval = jsonvalue.Split(':')[1];
                        row["Key"] = jkey;
                        row["Value"] = jval;
                        dt.Rows.Add(row);
                    }


                   // key = item.Key;
                }

                if (val is JArray)
                {
                    //key = item.Key;
                    foreach (var it in val)
                    {
                        // var newkey=
                        DataRow row = dt.NewRow();
                        string jsonvalue = Convert.ToString(it).Replace("[", "").Replace("]", "").Replace("{", "").Replace("}", "").Replace("\"", "");
                        //Convert.ToString(test);
                        string jkey = jsonvalue.Split(':')[0];
                        string jval = jsonvalue.Split(':')[1];
                        row["Key"] = jkey;
                        row["Value"] = jval;
                        dt.Rows.Add(row);
                    }
                }
            }

I am using asp.net , C#, Json.net , if anyone have any idea so please guide me how can i get my desired result..

我正在使用asp.net, c#, Json.net,如果有人有任何想法,请指导我如何得到我想要的结果。

1 个解决方案

#1


1  

I used this code recently to traverse an arbitrary JSON string. Each element is dumped with a row of dots at the beginning indicating its level in the hierarchy. You could modify it to output to a DataTable as you walk through an JArray for example.

我最近使用此代码遍历任意JSON字符串。每个元素在开始时都用一排点来表示它在层次结构中的级别。例如,您可以在遍历JArray时将其修改为输出到DataTable。

public static void JsonFileDump(string path)
{
    //Parse the data
    string jsonStr = File.ReadAllText(path);
    JToken token = JToken.Parse(jsonStr);   // get parent token
    JsonTokenDump(token);
}

public static void JsonTokenDump(JToken node, int lvl = 0, string nodeName = null)
{
    if (nodeName != null)
        Console.WriteLine("{0}Node Name={1}, Type={2}", new string('.', lvl), nodeName, node.Type);
    else
        Console.WriteLine("{0}Node Type={1}", new string('.', lvl), node.Type);


    if (node.Type == JTokenType.Object)
    {
        foreach (JProperty child in node.Children<JProperty>())
        {
            JsonTokenDump(child.Value, lvl + 1, child.Name);
        }
    }
    else if (node.Type == JTokenType.Array)
    {
        foreach (JToken child in node.Children())
        {
            JsonTokenDump(child, lvl + 1);
        }
    }
}

#1


1  

I used this code recently to traverse an arbitrary JSON string. Each element is dumped with a row of dots at the beginning indicating its level in the hierarchy. You could modify it to output to a DataTable as you walk through an JArray for example.

我最近使用此代码遍历任意JSON字符串。每个元素在开始时都用一排点来表示它在层次结构中的级别。例如,您可以在遍历JArray时将其修改为输出到DataTable。

public static void JsonFileDump(string path)
{
    //Parse the data
    string jsonStr = File.ReadAllText(path);
    JToken token = JToken.Parse(jsonStr);   // get parent token
    JsonTokenDump(token);
}

public static void JsonTokenDump(JToken node, int lvl = 0, string nodeName = null)
{
    if (nodeName != null)
        Console.WriteLine("{0}Node Name={1}, Type={2}", new string('.', lvl), nodeName, node.Type);
    else
        Console.WriteLine("{0}Node Type={1}", new string('.', lvl), node.Type);


    if (node.Type == JTokenType.Object)
    {
        foreach (JProperty child in node.Children<JProperty>())
        {
            JsonTokenDump(child.Value, lvl + 1, child.Name);
        }
    }
    else if (node.Type == JTokenType.Array)
    {
        foreach (JToken child in node.Children())
        {
            JsonTokenDump(child, lvl + 1);
        }
    }
}