parse this json {"and" : [{"in":[{"field":["givenname"]},"jordan"]}, {"in":[{"field":["rolefield"]},"23"]}]} in csharp

You can use the Newtonsoft.Json library to parse the JSON string as follows:

main.cs
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

string json = "{\"and\" : [{\"in\":[{\"field\":[\"givenname\"]},\"jordan\"]}, {\"in\":[{\"field\":[\"rolefield\"]},\"23\"]}]}";

JObject obj = JObject.Parse(json);
JArray arr = (JArray)obj["and"];

foreach (JObject item in arr)
{
    JArray inArr = (JArray)item["in"];
    JObject fieldObj = (JObject)inArr[0];
    string field = (string)fieldObj["field"][0];
    string value = (string)inArr[1];
    Console.WriteLine(field + " = " + value);
}
497 chars
17 lines

This code will output:

main.cs
givenname = jordan
rolefield = 23
34 chars
3 lines

Note that the field property contains an array, so we access its first element with [0].

related categories

gistlibby LogSnag