1

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.

7
  • what is your entry? Commented Feb 11, 2019 at 14:23
  • 2
    Why not deserialise to a concrete type that makes your C# code much easier to write and read? Commented Feb 11, 2019 at 14:24
  • Can you Deserialize to a List<Item>? Commented Feb 11, 2019 at 14:26
  • 1
    You have response that is an object. You then serialize that to a JSON string. Then you deserialize it to a JArray. What is the type of response, and why can't you get the data directly from that? Commented Feb 11, 2019 at 14:28
  • whenever it pushes a new entry get count value. Commented Feb 11, 2019 at 14:34

1 Answer 1

2

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);
Sign up to request clarification or add additional context in comments.

2 Comments

No, you have now completely rewritten your question.
I have missed that I was deserialising my Json in the Execute method, Your answer helped me Thanks/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.