0

I have json string like below

"{\"Key\":3296,\"Value1\":\"Test1\",\"Value2\":\"City\",\"Value3\":\"TX\",\"Value4\":null,\"Value5\":null,\"Value6\":null}{\"Key\":3297,\"Value1\":\"Test2\",\"Value2\":\"PUD\",\"Value3\":\"TX\",\"Value4\":null,\"Value5\":null,\"Value6\":null}"

and corresponding C# class

public class MyObject
{
    public int Key { get; set; }
    public string Value1 { get; set; }
    public string Value2 { get; set; }
    public string Value3 { get; set; }
    public string Value4 { get; set; }
    public string Value5 { get; set; }
    public string Value6 { get; set; }
}

when i try to deserialize this string using Newtonsoft i get error

{"Cannot deserialize the current JSON object (e.g. {\"name\":\"value\"}) into type 'System.Collections.Generic.IEnumerable`1[MyObject]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.\r\nTo fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.\r\nPath 'Key', line 1, position 7."}

I know that the string is not well formatted for json array. This is how i m getting the string and i dont have control over it.

What would be a best option here to convet that string into IEnumerable of MyObject. The string can have one object also.

LP13
  • 30,567
  • 53
  • 217
  • 400
  • 1
    Your JSON string is not valid as it consists of two separate objects just sitting next to each other, like `{object1}{object2}`. You should put those into an array so it becomes `[{object1}, {object2}]` – James Oct 19 '17 at 16:53
  • as i said, i understand its not well formed. But that is how i receiving it – LP13 Oct 19 '17 at 17:23
  • Well, if the string is always a list of simple objects (with no nesting) you can probably replace all instances of `}{` with `},{`, wrap it in square brackets and you have a valid JSON string which you can deserialize w Newtonsoft. If the string contains any curly braces within quotes, or nesting, you have a much more difficult task, and I would suggest having the folks who send you the string *fix it to be valid JSON* (which IMO should be your first approach). – James Oct 19 '17 at 17:41
  • Looks like a duplicate of [What is the correct way to use JSON.NET to parse stream of JSON objects?](https://stackoverflow.com/q/26601594) or [Best ways to split a string with matching curly braces](https://stackoverflow.com/a/38429174/3744182) or [Line delimited json serializing and de-serializing](https://stackoverflow.com/q/29729063/3744182) or [Parsing large json file in .NET](https://stackoverflow.com/q/32227436/3744182). – dbc Oct 19 '17 at 21:03

1 Answers1

0

Per James's answer (I'm sure you have figured this out but here it is). This should work with only 1 object as well:

string json = <your string here>;
json = "[" + json;
json = json.Replace("}{", "},{");
json += "]";

Not sure off-hand if there's a built-in way to do this in json.net.

wazz
  • 4,953
  • 5
  • 20
  • 34