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.
 
Show your relevant code so we can get an idea of what you are trying to do?
write to another system
Not making sense. What other system. Explain?
I have checked the way I have set up my classes and they seem to be ok.
By who's standards? You're listing yourself as a beginner, so how do you know you've not made a mistake?
Is what I am trying to do totally wrong, is there a better way?
I don't know, you haven't explained to us how you are going about this. When you read the Json file, what do you read it into?
  • Streamreader?
  • Memoryreader?
  • Class object of the root json file?
Then there are the questions of what exactly you want to do with that data once you have it. The more we know, the more helpful we can be. Sure, we can throw code at you all day, but what is the point in that if we don't know what you are trying to do.
 
Hi, yes I suspect your right.
I notice the success gets set to true.
So that seems to work.

Null1.png


This is what I have done. The original RootObject works fine, but the trouble is with the QRootObject.

Null2.png


Thanks,
 

Attachments

  • Null1.png
    Null1.png
    22.9 KB · Views: 15
Hi, by looking at the Json visualizer. I see something that I have had a brewing question about and may also be the answer to these issues.
There are a number of records and I wondered how they were dealt with.
The Json visualiser has given me some indication. "[x]", the payloads do not indicate this and so far the payloads has been all I have had to go off.
I have never had to deal with this scenario of a number of records, so far.
So how are these dealt with in code?
Thanks,

Null3.png
 
In the future, please post your code in code tags, not as screenshots.
 
Hi,
I am guessing something like this. But, what if you don't know how many records you will get?
Thanks,

C#:
"Account": [
        {
            "cableunit_name": "",
            "cableunit_number": "",
            "potential_installation": "",
            "municipality": "",
            "postcode": "",
            "customer_segment": "",
            "Housing_type": "",
            "building_type": "",
            "housing_ownership": ""
        },
        {
            "cableunit_name": "",
            "cableunit_number": "",
            "potential_installation": "",
            "municipality": "",
            "postcode": "",
            "customer_segment": "",
            "Housing_type": "",
            "building_type": "",
            "housing_ownership": ""
        },
 
I have something like the below.
So how do I get to some sort of ?
Thanks,

C#:
public class Record
        {
        public string test1 { get; set; }
        public string test2 { get; set; }
        }
 
No... You want to declare the field as either an array or a list when you don't know how many records will get.

I don't have the JSON.NET documentation handy right now, but I'm quite sure there is a section in the documentation regarding this.
 
Last edited by a moderator:
Hi, I must admit I am getting confused at the moment. The more articles I read the worst it gets.
I have something similar to the below. I already have a root object. the new root object I am trying to call QRootObject as above.

C#:
{
    "success": true,
    "payload": {
        "Data": {
            "record": [{
                    "field1": "",
                    "field2": "364",
                    "field3": "",
                }, {
                    "field1": "",
                    "field2": "364",
                    "field3": "",
                }, {
                etc

I have also tried

C#:
public class Data
        {
            //public Record record { get; set; }
            public List<Record> record { get; set; }
        }

QRootObject QRoot = JsonConvert.DeserializeObject<List<Record>>(response.Content);
 
When you insert code you can select the language type:
1601361737351.png
 
Hi, I have got something like the below. However, I quite sure it is incorrect.
Can anyone point me in the right direction please
Thanks,

C#:
       public class QPayLoad
        {
            public Data data { get; set; }
        }

        public class Data
        {
            //public Record record { get; set; }
            public IList<Record> record { get; set; }
        }

        public class QRootObject
        {
            public bool success { get; set; }
            //public string action { get; set; }
            public Header header { get; set; }
            public QPayLoad qpayload { get; set; }
        }

        public class Record
        {
        public string field1 { get; set; }
        public string field2 { get; set; }
        }
 
Hi, using Instantly parse JSON in any language | quicktype
Gives me the below;

Is trying to use QRootObject not a possibility?
I notice the line - public Record[] Record { get; set; }
Thanks,

C#:
namespace QuickType
{
    using System;
    using System.Collections.Generic;

    using System.Globalization;
    using Newtonsoft.Json;
    using Newtonsoft.Json.Converters;

    public partial class J
    {
        [JsonProperty("success")]
        public bool Success { get; set; }

        [JsonProperty("payload")]
        public Payload Payload { get; set; }
    }

    public partial class Payload
    {
        [JsonProperty("Data")]
        public Data Data { get; set; }
    }

    public partial class Data
    {
        [JsonProperty("record")]
        public Record[] Record { get; set; }
    }

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

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

        [JsonProperty("field3")]
        public string Field3 { get; set; }
    }
}
 
I can get the root object using the following code:
C#:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;

namespace SimpleCS
{
    class Root
    {
        public bool success { get; set; }
        public Payload Payload { get; set; }
    }

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

    class Data
    {
        public List<Record> Record { get; set; }
    }

    class Record
    {
        public string field1 { get; set; }
        public string field2 { get; set; }
        public string field3 { get; set; }
    }

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

            var root = JsonConvert.DeserializeObject<Root>(jsonText);
        }
    }
}

I was using this as my data:
response.json:
{
  "success": true,
  "payload": {
    "Data": {
      "record": [
        {
          "field1": "",
          "field2": "364",
          "field3": ""
        },
        {
          "field1": "",
          "field2": "364",
          "field3": ""
        }
      ]
    }
  }
}

Perhaps I'm not understanding the question.
 
Back
Top Bottom