1
[
   {
      "address":"addr1qx8trv7wualsnpj7xt"
   },
   {
      "address":"addr1qxkyl8vvmvy92ngm0y"
   },
   {
      "address":"addr1qywshwl6ud2myc0k2z"
   }
]

The above is a JSON file I'm trying to deserialize and read via NewtonsoftJSON.

However, I get the following error

'Unexpected character encountered while parsing value: {. Path '', line 1, position 2.'

What am I doing wrong? All I want to do is parse all addresses and put them in a list.

Full Code:

using Newtonsoft.Json;
using System;
using System.Collections.Generic;

namespace TestJSON
{
    class Program
    {
        static void Main(string[] args)
        {
            string json = @"[{""address"":""addr1qx8trv7wualsnpj7xt""}, {""address"":""addr1qxkyl8vvmvy92ngm0y""}, {""address"":""addr1qywshwl6ud2myc0k2z""}]";

            List<string> addresses = JsonConvert.DeserializeObject<List<string>>(json);

            foreach (string address in addresses)
            {
                Console.WriteLine(address);
            }
            Console.WriteLine(addresses);
        }
    }
}
1
  • 1
    create a class called address that as a string property called address then do List<address > addresses = JsonConvert.DeserializeObject<List<address >>(json); foreach (address a in addresses) { Console.WriteLine(a.address ); } Commented May 4, 2021 at 19:44

2 Answers 2

2

The quick way to do this is to define an anonymous type that has an address property, then use DeserializeAnonymousType.

string json = @"[{""address"":""addr1qx8trv7wualsnpj7xt""}, {""address"":""addr1qxkyl8vvmvy92ngm0y""}, {""address"":""addr1qywshwl6ud2myc0k2z""}]";
string[] list = JsonConvert.DeserializeAnonymousType(json, new[] { new { address = "" } }).Select(x => x.address).ToArray();

foreach (var item in list) Console.WriteLine(item);

Output:

addr1qx8trv7wualsnpj7xt
addr1qxkyl8vvmvy92ngm0y
addr1qywshwl6ud2myc0k2z
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! This worked, as did the one below. I'll be using that since I understand it better, but I'll definitely look into what the AnonymousType thing is. 👍
0

create a class called address that as a string property called address then do

List<address > addresses = JsonConvert.DeserializeObject<List<address >>(json); 
foreach (address a in addresses) 
{ 
  Console.WriteLine(a.address ); 
} 

public class address 
{
   private string _address ="";

    // Declare a address property of type string:
    public string address 
    {
        get
        {
            return _address ;
        }
        set
        {
            _address = value;
        }
    }

}

4 Comments

Thank you for this, I realized what I was doing wrong 👍
@Slaypoons you are welcome, please mark the answer up if it is useful
Says I need reputation :/
@Slaypoons not really, just to let others use the answer, I marked the one below as up as I like AnonymousType.

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.