0

When converting JSON to string (2nd method), I'm getting the error:

Newtonsoft.Json.JsonReaderException: 'Unexpected character encountered while parsing value: [. Path '', line 1, position 1.'

Why I'm getting an error in the 2nd method but the code is working fine in the 1st method, is there any solution for the 2nd method as I have to work with that method only?

Code:

static void Main(string[] args)
{
   string abc = "[{\"provider\":\"test\",\"schemes\":[{\"tyo\":\"1\",\"check\":\"99\",\"slotNumber\":\"0\"},{\"tyo\":\"2\",\"check\":\"99\",\"slotNumber\":\"469\"}]}]";

   var value = abc.FromJson().ToString();

   // Getting error in below line
   var value2 = abc.FromJson(typeof(String));       
}     

// First Method                    
public static object FromJson(this string json)
{
   var value = JsonConvert.DeserializeObject(json);
   return value;
}

// Second Method
public static object FromJson(this string json, Type type)
{
   var value = JsonConvert.DeserializeObject(json, type);     
   return value;
}                    
Rehan Shah
  • 1,505
  • 12
  • 28
rohit singh
  • 1,239
  • 3
  • 15
  • 26
  • Click [here](https://stackoverflow.com/questions/23259173/unexpected-character-encountered-while-parsing-value"here")! Check that i guess it's your problem. – Mikev Jan 03 '19 at 12:25
  • 2
    I don't understand why you're trying to deserialize Json to a string as it's already a string! What are you trying to do? – stuartd Jan 03 '19 at 12:30
  • 1
    Your question is misleading. You claim that you're getting an error in "the 2nd method", but your comments label it as "1st method". Are you just confused by your own comments in your debugging? – David Jan 03 '19 at 12:33
  • The example is just a sample console application. What I'm doing is i'm trying to convert the sample json abc to string by using the 2nd method which throws me error because my string contains backslash. – rohit singh Jan 03 '19 at 12:34
  • 1
    Your extension methods are pretty pointless. All they're really doing is making your code harder to read – Liam Jan 03 '19 at 12:34
  • Sorry, i updated the code. The comments were typed wrong, have pointed the right methods now. – rohit singh Jan 03 '19 at 12:37
  • Your sample JSON already is a string, there is no need to convert it to a string. – Christoph Sonntag Jan 03 '19 at 12:39
  • Your application is not compiling. It is not possible to call an extension function inside a static function. Provide a working code – Prasanth V J Jan 03 '19 at 12:46
  • This is just a sample, trying to understand the logic basically – rohit singh Jan 03 '19 at 12:47

1 Answers1

4

JsonConvert.DeserializeObject(string, Type) tries to parse JSON to the given type, assigning the properties of your object to properties of the resulting type. As String does not provide the necessary properties (In your case it probably needs to be an array with objects that provide properties like provider and schemes) it cannot be deserialized to a string.

This works as the deserialization to an array of objects is supported by Newtonsoft.Json :

var value2 = abc.FromJson(typeof(object[]));
Christoph Sonntag
  • 4,459
  • 1
  • 24
  • 49