1

I have a windows service, which loads parameters from database. There is a parameter is a Json string which has a field that is another Json string.

{ 
    "dll_fullname": "ScheduledTask.DialogTechSync.DialogTechSyncTask",
    "dll_para_json": "{"data_length": "5000", "max_parallelism": "4", "pause_time": "20"}"
}

In the above example, dll_para_json is the Json string in this Json string.

In the C# code, if I hardcode it as below, it works fine:

{ \"dll_fullname\": \"ScheduledTask.DialogTechSync.DialogTechSyncTask\",\"dll_para_json\": \"{\"data_length\": \"5000\", \"max_parallelism\": \"4\", \"pause_time\": \"20\"}\" }

However, if I put it in database, it has exception when I deserialize it:

dynamic results = JsonConvert.DeserializeObject<dynamic>(json);

I also tried to put it as below

{ \\"dll_fullname\\": \\"ScheduledTask.DialogTechSync.DialogTechSyncTask\\",\\"dll_para_json\\": \\"{\\"data_length\\": \\"5000\\", \\"max_parallelism\\": \\"4\\", \\"pause_time\\": \\"20\\"}\\" }

{ \\\"dll_fullname\\\": \\\"ScheduledTask.DialogTechSync.DialogTechSyncTask\\\",\\\"dll_para_json\\\": \\\"{\\\"data_length\\\": \\\"5000\\\", \\\"max_parallelism\\\": \\\"4\\\", \\\"pause_time\\\": \\\"20\\\"}\\\" }

{ \\\\"dll_fullname\\\\": \\\\"ScheduledTask.DialogTechSync.DialogTechSyncTask\\\\",\\\\"dll_para_json\\\\": \\\\"{\\\\"data_length\\\\": \\\\"5000\\\\", \\\\"max_parallelism\\\\": \\\\"4\\\\", \\\\"pause_time\\\\": \\\\"20\\\\"}\\\\" }

None worked. Always the same exception:

Error in parsing job: Newtonsoft.Json.JsonReaderException: After parsing a value an unexpected character was encountered: d. Path 'dll_para_json', line 1, position **.
at Newtonsoft.Json.JsonTextReader.ParsePostValue(Boolean ignoreComments)
at Newtonsoft.Json.JsonTextReader.Read()
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateJObject(JsonReader reader)
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)

Anyone knows how to fix this?

Thanks

urlreader
  • 5,221
  • 5
  • 47
  • 75
  • 3
    Clearly the json string you attempt to deserialize (which came from a DB, if i understand you correctly) is malformatted/illegal, as the exception tells you. Now, if you have problem with that string that is in that `json` variable, you should take a very close look at it, at every single character in it. All i can tell is that it obviously will not look exactly as the hardcoded string you tried (with respect to escaped or non-escaped characters that are part of json syntax). What precisely is the difference, you ask? How should i know, i don't know what exactly the `json` variable contains ;) –  May 13 '19 at 18:07
  • Why escape it in the first place? Maybe you can just have a composite json structure "dll_para_json": { "someKey":"SomeValue" } – Zakk Diaz May 13 '19 at 18:37
  • Your JSON is not well-formed, Json.NET will not be able to read it. When JSON is embedded inside JSON as a nested string literal, it should be properly escaped, e.g. as shown in [How do I convert an escaped JSON string within a JSON object?](https://stackoverflow.com/q/39154043) or [Json deserialization fails](https://stackoverflow.com/a/56053554/3744182). You need to fix the serializer that created the input JSON. – dbc May 13 '19 at 22:21
  • When initially generating the JSON, you can either escape the value of `"dll_para_json"` properly -- or write it as embedded JSON rather than as a string. If you were using Json.NET to write that JSON, see [Newtonsoft Json serialize a class where one of the properties is JSON](https://stackoverflow.com/a/32293568). – dbc May 13 '19 at 22:23

0 Answers0