Unexpected character encountered while parsing value - C#.Net JSON parsing

C#.Net Error 2

In this example we are going to read the JSON string using Newtonsoft.Json. You can download Newtonsoft.Json reference from NuGet package. 

Error Message:


Newtonsoft.Json.JsonReaderException
  HResult=0x80131500
  Message=Unexpected character encountered while parsing value: R. Path 'Name', line 1, position 8.
  Source=Newtonsoft.Json
  StackTrace:
   at Newtonsoft.Json.JsonTextReader.ParseValue()
   at Newtonsoft.Json.JsonTextReader.Read()
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateObject(Object newObject, JsonReader reader, JsonObjectContract contract, JsonProperty member, String id)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent)
   at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType)
   at Newtonsoft.Json.JsonConvert.DeserializeObject(String value, Type type, JsonSerializerSettings settings)
   at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value, JsonSerializerSettings settings)
   at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value)
   at ConsoleApp1.Program.Main(String[] args) in C:\Users\rajeshk\source\repos\ConsoleApp1\ConsoleApp1\Program.cs:line 16


Error Description:

using Newtonsoft.Json;
using System;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            string empData=@"{'Name':Ranga','Salary':'100'}";

            var details = JsonConvert.DeserializeObject(empData);
            Console.WriteLine("EmpName:" + details.Name + " with Salary" + details.Salary);
        }
    }
    class Employee
    {
        public string Name { get; set; }
        public int Salary { get; set; }
    }
}

In the above code there is a issue while righting data in JSON format. So parsing was not happened at Name parameter level as shown in the error.

Unexpected character encountered while parsing value: R. Path 'Name'

Solution:

Need to check the JSON format properly and run the below code again.
using Newtonsoft.Json;
using System;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            string empData=@"{'Name':'Ranga','Salary':'100'}";

            var details = JsonConvert.DeserializeObject(empData);
            Console.WriteLine("EmpName:" + details.Name + " with Salary" + details.Salary);
        }
    }
    class Employee
    {
        public string Name { get; set; }
        public int Salary { get; set; }
    }
}



Output: 



Share this post :

Post a comment

Please share your valuable feedback and share this article in social media.

 
Support : Ranga Rajesh Kumar
Copyright © 2019. Error Solution - All Rights Reserved
Site Designed by Ranga Rajesh Kumar