Answered Json De-serialise different root objects

AussieBoy

Well-known member
Joined
Sep 7, 2020
Messages
78
Programming Experience
Beginner
Hi,
I have code that reads values via Json using Root and RootObject.
It works fine.

However, I now want to write to another system with in the same program, with different classes.
I have tried something like below. Which I get 'Object reference not set to an instance of an object.'.
I have checked the way I have set up my classes and they seem to be ok. May be I have not picked up on something.
Is what I am trying to do totally wrong, is there a better way? I have read a few posts on other sites, but don't get what they are suggesting at the moment.
Thanks,

Code:
QRootObject QRoot = JsonConvert.DeserializeObject<QRootObject>(response.Content); //Newtonsoft.Json.
 
Hi, I cant see how to implement that into this program.
I think I am getting back to my original question as to whether it is possible to use QRootObject, QPayload etc.
Thanks,
 
JsonConvert.PopulateObject(json, account);

What on earth is so difficult about passing your json text to where it says json and in the account section, you would simply replace that and put the class name of the reference for which you want to populate?
 
Look at your Json man :
JSON:
        {
          "field1": "",
          "field2": "364",
          "field3": ""
        },
You have three fields. So which class object can also take three fields?
 
Ditch the classes you have and rewrite the logic. I fail to see why you can't use one class and populate the fields which match the content of the json file you are trying to read.
If you look close at your code you can also see how you could then add a list to that class as @Skydiver previously suggested on p1/#9. And for each record added to your record class, you could have a List<T> where T is the class object of your records...thus gives you a list of all your records.
 
If your Json file has multiple entries, then you need to factor in those entries as fields in your class object which you would then use the method i sent you to populate the items of that class via Populate an Object
 
From post #14, if you change line 40 with:
C#:
var root = new Root();
JsonConvert.PopulateObject(jsonText, root);
It will also work equally as well.
 
Look at your Json man :
JSON:
        {
          "field1": "",
          "field2": "364",
          "field3": ""
        },
You have three fields. So which class object can also take three fields?
Just to elaborate on what I was pointing out. I was using one of your class objects as an example on how to populate three fields from a class object. If you want to populate many fields, then you need to add those fields to your class which gets populated by the PopulateObject method.

And regarding the records, and not knowing how many records you have in the Json file, the above (p2/#21) should work fine. However, it's just my opinion, that I would have rewrote your logic a little differently and I may have kept a list of your records instead.

Anyway, try the example @Skydiver provided. Hopefully it does what you want?
 
Hi, I stripped out all existing code so I could use RootObject, Payload etc as to QRootObject, QPayload etc.
And managed to get it working to a point. Which brings me further problems to sort out later.
Thanks,
 
Hi, I will do. About to take a break.
I have read articles. Does anybody know if I cannot use QRootObject, QPayload etc.
Or should that be ok to do?
 
Here's a freebie for our OP since our fearless leader has told us to be nice to our users:
C#:
using System;
using System.IO;
using Newtonsoft.Json;

namespace SimpleCS
{
    class QRoot
    {
        [JsonProperty("success")]
        public bool Success { get; set; }
        public QPayload Payload { get; set; }
    }

    class QPayload
    {
        public Data Data { get; set; }
    }

    class Data
    {
        public Record[] Record { get; set; }
    }

    class Record
    {
        [JsonProperty("field1")]
        public string Field1 { get; set; }

        [JsonProperty("field2")]
        public long Field2 { get; set; }

        [JsonProperty("field3")]
        public string Field3 { get; set; }
    }

    class Program
    {
        static QRoot LoadByDeserialize(string jsonText)
            => JsonConvert.DeserializeObject<QRoot>(jsonText);

        static QRoot LoadByPopulate(string jsonText)
        {
            var root = new QRoot();
            JsonConvert.PopulateObject(jsonText, root2);
            return root;
        }

        static void Main(string[] args)
        {
            var jsonText = File.ReadAllText("response.json");

            var root1 = LoadByDeserialize(jsonText);
            Console.WriteLine(root1.Payload.Data.Record[0].Field2);

            var root2 = LoadByPopulate(jsonText);
            Console.WriteLine(root2.Payload.Data.Record[0].Field2);
        }
    }
}

All I did was change the class names and put in the appropriate JsonPropertyAttribute's for the places where the field names had a mismatch with the JSON. Otherwise it's the essentially the same code from posts #9 and #14.
 
Sorry for the typo on line 44. root2 should be root.
 
Back
Top Bottom