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.

Cœur
  • 37,241
  • 25
  • 195
  • 267
  • 1
    Shouldn't `public string bic` be `public string name`? – Patrick Roberts May 06 '18 at 00:34
  • 2
    Possible duplicate of [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – zaitsman May 06 '18 at 00:35
  • Try using `JsonConvert.DeserializeObject(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. – Scott Salyer May 06 '18 at 01:01
  • did you try .toList()? – TAHA SULTAN TEMURI May 06 '18 at 08: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. – Tomas Forsman May 06 '18 at 08:13
  • @TAHASULTANTEMURI What I've tried is: Accounts accs = JsonConvert.DeserializeObject(json); ||| List accs = JsonConvert.DeserializeObject>(json); ||| var accs = JsonConvert.DeserializeObject(json); I'm not sure how to implement .ToList() here. – Tomas Forsman May 06 '18 at 08:18
  • ok i give you solution wait please – TAHA SULTAN TEMURI May 06 '18 at 08:21
  • @zaitsman I've checked that possible duplicate before posting but couldn't figure out why I get the exception. I've been racking my brain, hoping that I've just missed something very simple, but after 12 hours of fighting and comparing what have helped others with similar problems I finally accepted that I need help solving this. When I figure this out I'll go back to that solution and see if I can use that to further explain where I went wrong for others that encounters the same issue. – Tomas Forsman May 06 '18 at 08:25
  • @PatrickRoberts Yes, I changed bic to name before posting here but forgot to change it in code as well. It is not part of the problem but thanks for pointing it out. – Tomas Forsman May 06 '18 at 08:27
  • so you just want to print all ids only? – TAHA SULTAN TEMURI May 06 '18 at 08:27
  • nothing wrong with this code ? this is working fine – TAHA SULTAN TEMURI May 06 '18 at 08:31

1 Answers1

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();
    }
TAHA SULTAN TEMURI
  • 4,031
  • 2
  • 40
  • 66
  • 1
    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? – Tomas Forsman May 06 '18 at 09:08
  • 1
    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. – Tomas Forsman May 06 '18 at 09:12
  • @Thomas I have tested this code in both .net core and .net version, and its working fine. – TAHA SULTAN TEMURI May 06 '18 at 13:05