0

I have this json file :

{
   “transactions”:[
      {
         “type”:”deposit”,
         “account_id”:123456789012345,
         “amount”:20000.0
      },
      {
         “type”:”deposit”,
         “account_id”:555456789012345,
         “amount”:20000.0
      },
      {
         “type”:”payment”,
         “account_id”:123456789012345,
         “amount”:20000.0
      },
      {
         “type”:”transfer”,
         “from”:555456789012345,
         “to”:123456789012345,
         “amount”:20000.0
      }
   ]
}

and I want to read this file with JSON.net.

I tried this code but it has some unhandeld errors :

var records = JsonConvert.DeserializeObject<List<Type>>(File.ReadAllText(FileAddress));
using (StreamReader SrFile = File.OpenText(FileAddress))
{
    JsonSerializer Serializer = new JsonSerializer();
    JsonAccount newJsonAccount = (JsonAccount)Serializer.Deserialize(SrFile, typeof(JsonAccount));
}

the error is :

An unhandled exception of type 'Newtonsoft.Json.JsonReaderException' occurred in Newtonsoft.Json.dll

Additional information: Unexpected character encountered while parsing value: �. Path '', line 0, position 0

now it has this error :

Additional information: Error converting value "transactions" to type 'ImportAndExport.MainForm+JsonAccount'. Path '', line 1, position 14.

UPDATE 2 :

now the error is :

    An unhandled exception of type 'Newtonsoft.Json.JsonReaderException' occurred in Newtonsoft.Json.dll

Additional information: Invalid character after parsing property name. Expected ':' but got: t. Path '', line 2, position 7.
1
  • 1
    Where are you using var records ? Commented Aug 12, 2014 at 9:43

1 Answer 1

2

Your error:

Additional information: Unexpected character encountered while parsing value: �. Path '', line 0, position 0.

Is probably caused by your using curly quotes instead of straight quotes. Replace it with standard straight quotes.

EDIT:

In case you get more issues with JSON parsing, this is how I would parse that JSON:

         string jsonString = @"{
    ""transactions"": [
        {
            ""type"": ""deposit"",
            ""account_id"": 123456789012345,
            ""amount"": 20000
        },
        {
            ""type"": ""deposit"",
            ""account_id"": 555456789012345,
            ""amount"": 20000
        },
        {
            ""type"": ""payment"",
            ""account_id"": 123456789012345,
            ""amount"": 20000
        },
        {
            ""type"": ""transfer"",
            ""from"": 555456789012345,
            ""to"": 123456789012345,
            ""amount"": 20000
        }
    ]
}";

        var recordObject = JObject.Parse(jsonString);

If I wanted to get the last transaction

        var lastRecord = JObject.Parse(jsonString)["transactions"].Last()

If I wanted the deposit records

        var deposits = from transactions in JObject.Parse(jsonString)["transactions"]
                       where transactions["type"].ToString().Equals("deposit")
                       select transactions;
Sign up to request clarification or add additional context in comments.

4 Comments

Please tell me more about it ... I can not understand
instead of “type”:”deposit” it should be "type":"deposit". You should replace all instances of and with "
Thank you very much. But there is another error ! See update2
That seems like a bad JSON format error. Go to jsonlint.com and paste your JSON there and see if it validates :)

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.