Reputation: 2871
I have this json body
[
{
"Id": 1,
"Name": "John",
"Icon": "Icon/someplace",
"SortOrder": 1
},
{
"Id": 2,
"Name": "Jessica",
"Icon": "Icon/someplace",
"SortOrder": 1
},
{
"Id": 3,
"Name": "Kevin",
"Icon": "Icon/someplace",
"SortOrder": 1
},
{
"Id": 4,
"Name": "Lint",
"Icon": "Icon/someplace",
"SortOrder": 1
},
{
...
}
]
I am adding Values to the json via API, I need to verify that the new value is present is the json body
I am trying to Covert the response to json,
public object Get_Json()
{
var response = GEt_Json_Body();
var json_string = JsonConvert.SerializeObject(response);
JArray UpdatedContent = JArray.Parse(json_string);
JObject Facility_Json = JObject.Parse(UpdatedContent[0].ToString());
Assert.NotNull(Facility_Json);
return Facility_Json;
}
This Only gives me back the first json:
{
"Id": 1,
"Name": "John",
"Icon": "Icon/someplace",
"SortOrder": 1
}
UpdatedContent[i]
i allows me to get the other jsons in the array, the problem is I don't know where the json I Created using the API will be placed, how to get All of the JArray and verify that my entry is there?
Update:
This is my Call:
public List<FooModel> Get_Json_Body()
{
var request = new RestRequest();
request.Resource = string.Format("/api/get_something");
return Execute<FooMedl>(request, Endpoint);
}
public class FooModel
{
public int Id { get; set; }
public string Name { get; set; }
public string Icon { get; set; }
public int SortOrder { get; set; }
}
public List<T> Execute<T>(RestRequest request, string Endpoint) where T : new()
{
Client.BaseUrl = new Uri(Endpoint);
var response = Client.Execute<List<T>>(request);
Console.WriteLine(response.ResponseUri);
if (response.ErrorException != null)
{
string message = String.Format("Error retrieving response: \n{0} \n{1} \n{2} ",
response.Content, response.ErrorMessage, response.ErrorException);
Console.WriteLine(message);
var exception = new ApplicationException(message);
throw exception;
}
return Response.Data;
}
Update 2:
The Answer By Davig G helped me to solve the problem, Was able to verify my input via
if(data.Any(f => f.Name == "Kevin"))
{
Console.WriteLine("Kevin exists in the data");
}
I am returing a List of dictionaries From Get_Json() method Using DavigG's answer I am able to verify and access the specific keys and values within the list.
Upvotes: 1
Views: 182
Reputation: 119186
It's much easier to deal with concrete classes than pure JSON objects, I would recommend deserialising your JSON directly into a list of objects like this:
public class Foo
{
public int Id { get; set; }
public string Name { get; set; }
public string Icon { get; set; }
public int SortOrder { get; set; }
}
And the code to deserialise is:
var data = JsonConvert.DeserializeObject<List<Foo>>(response);
Now you can treat that object as you wish, for example:
if(data.Any(f => f.Name == "Kevin"))
{
Console.WriteLine("Kevin exists in the data");
}
Or even make modifications:
var kevin = data.SingleOrDefault(f => f.Name == "Kevin");
if(kevin != null)
{
kevin.SortOrder = 3;
}
And then if you need it to be back to the original JSON:
var json = JsonConvert.SerializeObject(data);
Upvotes: 2