-2

I have a JSON string like

string str = [{"a":"b"},{"c":"d"}],

I converted it to Jarray with the below code,

JArray obj=JArray.Parse(str);

Its working fine, but my input string got changed to

str=[{"a":"b"},{"c":"d"}],[{"e":"f"},{"g":"h"}]

Now the JArray parsing is throwing me an error as its a list of JArray, How can i parse the above string in C#?

Ashok
  • 1
  • Throws what? What is the *actual* code, actual string? `string str = [{"a":"b"},{"c":"d"}],` is neither valid C# nor valid JSON and definitely not a JSON array – Panagiotis Kanavos Nov 01 '18 at 15:23
  • If the string contained a valid array JSON.NET would parse it without problem. – Panagiotis Kanavos Nov 01 '18 at 15:24
  • Your last string is not a valid JSON string, you can validate it in https://jsonlint.com/ – Alfredo A. Nov 01 '18 at 15:27
  • 1
    OK, that's NOT valid JSON. You *can't* have multiple root elements in a JSON string : `[{"a":"b"},{"c":"d"}], [{"e":"f"},{"g":"h"}]`. That's two separate JSON fragments. An array or arrays would surround this with square brackets, ie `[ [{"a":"b"},{"c":"d"}], [{"e":"f"},{"g":"h"}] ]` – Panagiotis Kanavos Nov 01 '18 at 15:27
  • 1
    `[{"a":"b"},{"c":"d"}],[{"e":"f"},{"g":"h"}]` is not valid json – Rui Jarimba Nov 01 '18 at 15:28
  • Post the *actual* code and strings. Where does this string come from? The code that generates it will have to be fixed, or the string will have to be converted to valid JSON – Panagiotis Kanavos Nov 01 '18 at 15:29
  • Looks like a duplicate of [Additional text encountered after finished reading JSON content:](https://stackoverflow.com/q/16765877/3744182). Either fix your JSON to have outer brackets as shown in [this answer](https://stackoverflow.com/a/16765884/3744182), or use `new JArray(JsonExtensions.DeserializeDelimitedJson(new StringReader(str)).SelectMany(a => a))` where `JsonExtensions.DeserializeDelimitedJson` is taken from [this answer](https://stackoverflow.com/a/50014780/3744182). – dbc Nov 01 '18 at 18:43

1 Answers1

0

I was able to find a way to parse the following string using JsonReader.SupportMultipleContent:

[{"a":"b"},{"c":"d"}],[{"e":"f"},{"g":"h"}]

Thanks @dbc for pointing me in the right direction (see Additional text encountered after finished reading JSON content). Please note that this functionality is available only from Release 11.0.1 of Json.NET.

The following method uses a JsonTextReader and returns a IEnumerable<KeyValuePair<string, string>>:

private static IEnumerable<KeyValuePair<string, string>> ParseKeyValuePairs(string json)
{
    using (var reader = new StringReader(json))
    using (var jsonReader = new JsonTextReader(reader) { SupportMultipleContent = true })
    {
        JsonSerializer serializer = JsonSerializer.CreateDefault();

        while (jsonReader.Read())
        {
            if (jsonReader.TokenType == JsonToken.Comment)
            {
                continue;
            }

            var dictionaries = serializer.Deserialize<List<Dictionary<string, string>>>(jsonReader);

            foreach (KeyValuePair<string, string> keyValue in dictionaries.SelectMany(x => x))
            {
                yield return keyValue;
            }
        }
    }
}

Using the code to generate a Dictionary<string, string>:

string json = @"[{""a"":""b""},{""c"":""d""}],[{""e"":""f""},{""g"":""h""}]";

Dictionary<string, string> data = ParseKeyValuePairs(json).ToDictionary(x => x.Key, x => x.Value);

Result:

Dictionary

Rui Jarimba
  • 11,166
  • 11
  • 56
  • 86