Resolved Large Json response - displaying certain items

ConsKa

Well-known member
Joined
Dec 11, 2020
Messages
140
Programming Experience
Beginner
I am calling an API that is quite long, and despite my best efforts, I have been unable to get the examples to work and I am not entirely sure where to go from here.

I have no issue obtaining the json as a string. When I did json to classes, I get the below (though it is even longer).

C#:
public class ItemModel
{
    public class Rootobject
    {
        public Request request { get; set; }
        public Response_Status response_status { get; set; }
    }
   
    public class Request
    {
        public object testOne { get; set; }
        public string testTwo { get; set; }
        public Resolution resolution { get; set; }
        public Item item { get; set; }
    }
    public class Resolution
    {
        public object[] resolution_attachments { get; set; }
    }
    public class Item
    {
        public string name { get; set; }
        public string id { get; set; }
    }
}

Now my issue is I want to get the Name and ID from Item.

But after spending a couple of hours in google, stack overflow and even NewtonSoft's documentation, I can't get it to work.

I tried JObject, JToken neither seem to be able to retrieve a 3rd? level item from the Json - I say 3rd, because it appears that the Item class is within the Request class, which is itself within the Rootobject class. When I tried JObject/JToken (can't remember which now I have used both for hours) - I got an error that it was returning :{ - which means that it was not finding the underlying name but rather just the two characters after item in:

JSON:
"item":{ "name": "Bob", "id": "1"},

Any pointers would be gratefully received because my brain is beginning to hurt.

Edited to add the json example in its original form:

JSON:
{
    "request":
    {
        "test1": null
        "test2": "one"
        "test3":
        {
            "test4": 1
            "test5": 3
        },
        "Test6": null,
        "Test7":
        {
            "name": "Ted",
            "id": "tedID"
        },
        "item":
        {
            "name": "itemName"
            "id": "itemID"
        },
    }
}
 
Last edited:
Are you sure that is the JSON in its original form? It seems to missing commas in multiple places and has an extra comma in another place. Please give us the real thing or at least some code and corresponding JSON that minimally reproduces your problem. Trying to paraphrase or give inexact information is just going waste everybody's time.
 
Bit difficult to do that, it is huge.

I have done a better job of typing out a snippet below - i tried to prettify it in my first post to make it easier to read - but it goes on like this for pages and I am talking literally 3 pages of A4.

I was hoping that there was a way of doing it using the JSON to Classes paste special in Visual Studio. To give you an idea, there are 34 entries under Request and 29 separate classes under that.

I have tried JsonConvert, which looks like it is almost working....but returns a null value?

C#:
var json = await response.Content.ReadAsStringAsync();

Item token = JsonConvert.DeserializeObject<Item>(json);

This comes back as null, and I am not sure why.

When I breakpoint it, and look at the string in the JSON visualizer (json), it displays the request and in the list is "item" and under that "name" and "id" and both have a value at this stage.

token from the code above has "name" and "id" so it has grabbed the proper conventions, but both are null?

It has taken the public class Item, taken the properties from within, name and id, but for some reason has not assigned them as part of the deserialization process?

This is a snippet of the code, anonymised but typed accurately, though I am not sure how much help it will be.

removed the code as I wasn't really happy about posting that much of it...wasn't mine to post.
 
Last edited:
Always the way with me.....

Posting something must really help me thinking it through....but for anyone that comes across this:

C#:
var json = await response.Content.ReadAsStringAsync();

Rootobject token = JsonConvert.DeserializeObject<Rootobject>(json);

textbox1.Text = $"Returns { token.request.item.name }";"

Gives me what I wanted...
 
I'm glad that you figured it out.

If you are discovering that posting on a forum or preparing a follow-up post to a forum is helping you solve your own issue, I highly recommend reading:

There are lots of helpful bits in there that will help you prepare your question, and quite often in the process of preparing the question, help you zero in on the problem and find an answer yourself. I know I've found it really useful when I don't have a rubber duck.
 
I'm glad that you figured it out.

If you are discovering that posting on a forum or preparing a follow-up post to a forum is helping you solve your own issue, I highly recommend reading:

There are lots of helpful bits in there that will help you prepare your question, and quite often in the process of preparing the question, help you zero in on the problem and find an answer yourself. I know I've found it really useful when I don't have a rubber duck.

Wow reading that was like a trip back in time to when I first joined the internet cheers for that.

To kind of explain where I am...

1, I don't come here immediately, I search for hours, I read everything I can locate on the internet, to find a solution first. I come here when I just don't know where to go next.

2, I don't stop looking after posting. I continue to read, and search and test code to see if I can find an answer - because you guys might be busy.

3, Sometimes something you guys will ask me, will prompt me to remember something I have tried 2.5 hours ago and to reconsider it, and re-try it. Which happened here, I tried JsonConvert early on, but hit a brick wall, you saying, give us some code, made me go back to re-create that code - so that I could take a note of the errors. Which in turn, perhaps with some of the knowledge I had gained in the interim, led me to re-factor the code to try new things.

4, In relation to this question, if you knew deserialization and the "token.request.item.name" syntax - everything you needed to know to provide that answer is in the first question.

This in particular would have honed you in to that point:

"retrieve a 3rd? level item from the Json - I say 3rd, because it appears that the Item class is within the Request class, which is itself within the Rootobject class"

4, I have searched for hours and I have NEVER seen the syntax "token.request.item.name". I read Newtonsoft, Stack Overflow, Code Project, I googled 15 different things and went to numerous websites. Not once did I come across the idea that you can go beyond a "token.name" with json deserialization.

That could be a failure of my search skills, could be that it is difficult to search one thing, when another thing with a similar name/concept fills the first 5 pages of any search results (nested is not a good search term). It may be out there, but finding it? I have not managed to find any example of this so far.

I really like the rubber duck I will definitely use that in the future - essentially writing these posts was my rubber duck - but I will do the rubber duck before posting next time.
 
Back
Top Bottom