0

I have JSON on input, which contain arrays like this

[13806008,,[[27017723,,[0.25,-180,145],],[26683222,,[0,-125,106],]],0,"0","0","0","0",null,[[176,"673041"],[168,"2"],[175,"val"],[169,"1"]]]

Chrome Web Inspector parses those double commas like undefined elements, but the Newtonsoft Json library throws an exception with this format.

The only way that I see - insert null between double commas first and parse string then.

Is there faster way to parse such JSON strings?

Blorgbeard
  • 101,031
  • 48
  • 228
  • 272
  • Possible duplicate of [How to ignore a property in class if null, using json.net](http://stackoverflow.com/questions/6507889/how-to-ignore-a-property-in-class-if-null-using-json-net) – techspider Apr 28 '16 at 21:15
  • 2
    `I have JSON on input` -> I am afraid that you don't have a string JSON as input. JSON has pretty strict schema. What you have is a string which doesn't comply to any valid JSON schema. So it's normal that a standard JSON serializer will complain if you throw such invalid string as input. If you have some random string that doesn't comply to any specification or RFC you might need to write custom code to parse depending on its grammar. Simply forget about using a JSON serializer like the Newtonsoft.JSON library if you don't have a valid JSON input. – Darin Dimitrov Apr 28 '16 at 21:18

2 Answers2

0

As Darin Dimitrov says in a comment, this isn't JSON. So it's then up to you to figure out how you want to interpet it. From the example, it looks like a pretty simple 'subset' of JSON, so here's what I'd suggest.

I've written a library called canto34 which lets you write your own interpreters for simple language problems like this, and described a program to recognise nested lists of tokens -- in my case, lisp s-expressions, but these are very similar to nested JavaScript lists, just with different brackets. ;)

Here's the kind of structure you need to parse nested lists;

public class SExpressionParser : ParserBase
{
    internal dynamic SExpression()
    {
        if (LA1.Is(SExpressionLexer.OP))
        {
            Match(SExpressionLexer.OP);
        }
        else
        {
            var atom = Match(SExpressionLexer.ATOM).Content;
            return atom;
        }    

        var array = new List<dynamic>();
        while (!EOF && !LA1.Is(SExpressionLexer.CL))
        {
            array.Add(SExpression());
        }    

        Match(SExpressionLexer.CL);
        return array;
    }
}
Steve Cooper
  • 20,542
  • 15
  • 71
  • 88
0

Looking closely to my string I realized what that's regular JSON array! I simply parse my sting as JSON Array!

JArray JsonArray= JArray.Parse(responseString);