Json在C#中解析而不创建类

时间:2017-09-06 06:11:54

标签: c# json json-deserialization

我想读取给定的json并仅显示包含字符串"value"的数组名称的"PL_DATA_HL"参数值。

示例JSON:

{
    "PL_DATA_HL_XYZ": [
        {
          "name": "$.properties.start",
          "value": "new password"
        },
        {
          "name": "$.properties.end",
          "value": "2017-04-20T00:30:00Z"
        },
    ],
    "PL_DATA_IL_HGF": [
        {
          "name": "$.properties.start",
          "value": "2017-05-21T01:00:00Z"
        },
        {
          "name": "$.properties.end",
          "value": "2017-05-21T01:00:00Z"
        },
    ],
    "PL_DATA_HL_ABC": [
        {
          "name": "$.properties.start",
          "value": "new password"
        },
        {
          "name": "$.properties.end",
          "value": "2017-04-20T00:30:00Z"
        },
    ],
 }

我已经尝试使用下面的代码,但似乎无法正常工作!

dynamic jsonObj = Newtonsoft.Json.JsonConvert.DeserializeObject(json);

foreach (var set in jsonObj)
{
   Console.WriteLine(set.value);
}

5 个答案:

答案 0 :(得分:1)

重新检查结构,您的代码将如下所示:

dynamic jsonObj = JsonConvert.DeserializeObject(json);

foreach (var set in jsonObj)
{
    if(Convert.ToString(set.Name).Contains("PL_DATA_HL"))
        foreach (var sub in set.Value)
        {
            Console.WriteLine(sub.value);
        }
}

答案 1 :(得分:1)

您必须检查根对象中每个元素的属性名称。考虑到你必须检查属性名称,最好导航JObject而不是dynamic的属性(在这种情况下你应该使用Reflection)。

这样的事情应该有效:

JObject jsonObj = Newtonsoft.Json.JsonConvert.DeserializeObject<JObject>(json);
foreach (var property in jsonObj.Properties())
{
    if (property.Name.StartsWith("PL_DATA_HL"))
    {
        Console.WriteLine("Property: " + property.Name);

        JArray array = (JArray)property.Value;

        foreach (JObject values in array)
        {
            Console.WriteLine("Name: " + values.GetValue("name"));
            Console.WriteLine("Value: " + values.GetValue("value"));
        }

        Console.WriteLine("---------------------------------");
    }
}

Console.ReadKey();

答案 2 :(得分:0)

您可以尝试这样:

foreach (var jsonArr in jsonObj)
        {
            if (jsonArr.Key.StartsWith("PL_DATA_HL"))
            {
                foreach (var elem in jsonObj[jsonArr.Key])
                {
                    Console.WriteLine(elem["value"]);
                }
            }
        }

答案 3 :(得分:0)

我认为一种方法是像这样使用Regex

var values =
    Regex.Matches(json, @"""PL_DATA_HL[^]]+]")
        .OfType<Match>()
        .SelectMany(c=> Regex.Matches(c.ToString(), @"(?<=""value"")\s*:\s*""(?<value>[^""]+)""")
        .OfType<Match>().Select(m=> m.Groups["value"].ToString()))
        .ToList();

[ C# Demo ]

答案 4 :(得分:0)

简单代码:

string json = string.Empty;
using (StreamReader r = new StreamReader("Sample.json"))
{
    json = r.ReadToEnd();
}
dynamic jsonObj = Newtonsoft.Json.JsonConvert.DeserializeObject(json);

foreach (var set in jsonObj)
{
    if ((set).Name.Contains("PL_DATA_HL"))
    {
        object value = (set).Value;
        dynamic jsonValues = Newtonsoft.Json.JsonConvert.DeserializeObject(value.ToString());
        Console.WriteLine("Values for " + (set).Name);
        foreach (var jsonValue in jsonValues)
        {
            var items = Newtonsoft.Json.JsonConvert.DeserializeObject(jsonValue.ToString());
            foreach (var item in items)
            {
                if((item).Name == "value")
                    Console.WriteLine((item).Value);
            }
        }                    
    }
}