0

I've done my research and I've found several solutions to problems that are almost the same as mine but none of them have worked so far. What I'm trying to accomplish is to read a json file where the first item is an array, like this:

{
"the_array":[

The error I get is: Object reference not set to an instance of an object.

The closest I've gotten is when I try to write out the entire json but then I only get the first item in the array. [edit: clarification below]

Here's the code:

Program.cs

using Newtonsoft.Json;
using System;
using System.IO;

namespace JsonToXML
{
class Program
{
    static void Main(string[] args)
    {
        try
        {
            string json = File.ReadAllText(@"w:\code\csharp\JsonToXML\simple-sample.json").ToString();

            Accounts accs = JsonConvert.DeserializeObject<Accounts>(json);

          foreach (var acc in accs.account_list)
           {
            Console.Write(acc.id.ToString());
            Console.ReadKey(true);
        }
        catch(Exception ex)
        {
            Console.Write(ex.Message.ToString());
            Console.ReadKey(true);
        }
    }
}

}

Accounts.cs

using System.Collections.Generic;

namespace JsonToXML
{
class Accounts
{
    public List<accounts> account_list { get; set; }

    public class accounts
    {
        public string id { get; set; }
        public string bic { get; set; }
    }
  }
}

simple_sample.json

{
"account_list": [
    {
        "id": "AsdF01234EfgH4567",
        "bic": "A"
    },
    {
        "id": "AbcD1234eFgH568",
        "bic": "B"
    },
    {
        "id": "Baas786DD5886RT",
        "bic": "C"
    },
    {
        "id": "458A889B8889T784W",
        "bic": "D"
    }
]

}

I've also tried using List<Accounts> with no success.

If anything about the question is unclear, just let me know and I'll clarify as best I can.

I'm coding this in .Net Core

[edit: clarification from above] If I try:

var accs = JsonConvert.DeserializeObject<dynamic>(json);
Console.Write(accs.ToString());

The result is: { "id": "AsdF01234EfgH4567", "bic": "SWEDSESS" }


Solved

The problem was within the json file. While it looked like it worked, somehow sublime had saved it incorrectly. After remaking the json file with the exact same content, it worked as intended.

11
  • 1
    Shouldn't public string bic be public string name? Commented May 6, 2018 at 0:34
  • 2
    Possible duplicate of What is a NullReferenceException, and how do I fix it? Commented May 6, 2018 at 0:35
  • Try using JsonConvert.DeserializeObject<dynamic>(json); and see what that brings back. It'll give you a good indication on what the deserialization process is actually trying to do, which can point you in the right direction. Commented May 6, 2018 at 1:01
  • did you try .toList()? Commented May 6, 2018 at 8:08
  • 1
    @RubyHaus I realised I didn't explain that bit well at all. I did try that and I've added the result to the question since I can't figure out why the result is what it is. Commented May 6, 2018 at 8:13

1 Answer 1

1

Its simple you only GET FIRST because this `

Console.ReadKey(true)`

Readkey is used to wait for user key press so it wont execute further until you press some key read Here

Final Code

  try
        {
  string json = File.ReadAllText(@"w:\code\csharp\JsonToXML\simple-sample.json").ToString();

            Accounts accs = JsonConvert.DeserializeObject<Accounts>(json);

            foreach (var acc in accs.account_list)
            {
                Console.WriteLine(acc.id.ToString());


            }
        }

        catch (Exception ex)
        {
            Console.Write(ex.Message.ToString());
           throw ex;
        }
        Console.ReadKey();
    }
Sign up to request clarification or add additional context in comments.

3 Comments

For me, this is still not working and throwing: Object reference not set to an instance of an object. Are you running this as a Microsoft.NetCore.App and it's working for you?
I got it working now by remaking the json file. Seems to be an issue with how sublime saved the json file. Editing the question to include the solution.
@Thomas I have tested this code in both .net core and .net version, and its working fine.

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.