Viku
Viku

Reputation: 2973

NewtonSoft json parsing

Can somebody help me to parse the json and get the details. Lets say i have Top2 input parameter as Police and i want to know the respective Top3 and in that Top3 array i need to check whether Test1Child value is there or not. I am using newtonsoft json + c# and so far i can get till DeviceTypes using below code

 var json = File.ReadAllText(jsonFile); // below json is stored in file jsonFile
 var jObject = JObject.Parse(json);
 JArray MappingArray =(JArray)jObject["Top1"];
string strTop1 = ObjZone.Top1.ToString();
string strTop2 = ObjZone.Top2.ToString();
var JToken = MappingArray.Where(obj => obj["Top2"].Value<string>() == strTop2).ToList();

//Json

{
   "Top1": [
    {
      "Top2": "Test1",
      "Top3": [
        "Test1Child"
      ]
    },
    {
      "Top2": "Test2",
      "Top3": [
        "Test2Child"
      ]
    }
    ]
}

Upvotes: 0

Views: 115

Answers (1)

Jarek Kożdoń
Jarek Kożdoń

Reputation: 336

I'd use http://json2csharp.com/ (or any other json to c# parser) and then use C# objects (it's just easier for me) This would look like that for this case:

namespace jsonTests
{
    public class DeviceTypeWithResponseTypeMapper
    {
        public string DeviceType { get; set; }
        public List<string> ResponseTypes { get; set; }
    }

    public class RootObject
    {
        public List<DeviceTypeWithResponseTypeMapper> DeviceTypeWithResponseTypeMapper { get; set; }
    }
}

and the use it like that in code:

    var rootob = Newtonsoft.Json.JsonConvert.DeserializeObject<RootObject>(str);

    var thoseThatHaveNotUsed = rootob.DeviceTypeWithResponseTypeMapper.Where(dtwrtm =>
              dtwrtm.ResponseTypes.Any(rt => rt == "NotUsed"));

    foreach (var one in thoseThatHaveNotUsed)
    {
        Console.WriteLine(one.DeviceType);
    }

this code lists all the Device types that have "NotUsed" among the responses.

version 2 (extending your code) would look like that, i believe:

static void Main(string[] args)
{
       var json = str; // below json is stored in file jsonFile
    var jObject = JObject.Parse(json);
    JArray ZoneMappingArray = (JArray)jObject["DeviceTypeWithResponseTypeMapper"];
    string strDeviceType = "Police";
    string strResponseType = "NotUsed";
    var JToken = ZoneMappingArray.Where(obj => obj["DeviceType"].Value<string>() == strDeviceType).ToList();
    var isrespTypeThere = JToken[0].Last().Values().Any(x => x.Value<string>() == strResponseType);

    Console.WriteLine($"Does {strDeviceType} have response type with value {strResponseType}? {yesorno(isrespTypeThere)}");
}

private static object yesorno(bool isrespTypeThere)
{
    if (isrespTypeThere)
    {
        return "yes!";
    }
    else
    {
        return "no :(";
    }
}

result:

enter image description here

and if you'd like to list all devices that have response type equal to wanted you can use this code:

  var allWithResponseType = ZoneMappingArray.Where(jt => jt.Last().Values().Any(x => x.Value<string>() == strResponseType));

    foreach (var item in allWithResponseType)
    {
        Console.WriteLine(item["DeviceType"].Value<string>());
    }

Upvotes: 2

Related Questions