Ritesh
Ritesh

Reputation: 1036

Json Parsing in C# without creating class

I want to read a given json and display the value of "value" parameter only for the Array name that contains string "PL_DATA_HL".

Sample 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"
        },
    ],
 }

I already tried using below code, but seems not working!

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

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

Upvotes: 0

Views: 155

Answers (5)

shA.t
shA.t

Reputation: 16958

I think a way is to use Regex like this:

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 ]

Upvotes: 0

Vandita
Vandita

Reputation: 748

Simple code :

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);
            }
        }                    
    }
}

Upvotes: 0

Omar Muscatello
Omar Muscatello

Reputation: 1301

You have to check the property name of each element in the root object. Considering that you have to check the property name it's a better idea to navigate throught the properties of JObject instead of the dynamic (you should use Reflection in this case).

Something like this should work:

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();

Upvotes: 1

ASpirin
ASpirin

Reputation: 3651

Recheck the structure and your code will looks like:

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);
        }
}

Upvotes: 1

AlexK
AlexK

Reputation: 591

You can try like this:

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

Upvotes: 0

Related Questions