0

I`m trying to pars a Json in my C# code with Json.NET. I want to print the child of each token after the parent. my JSON string is something like this(but very longer):

 {
    "type": "ExpressionStatement",
    "expression": {
      "type": "CallExpression",
      "callee": {
        "type": "FunctionExpression",
        "id": null,
        "params": [
          {
            "type": "Identifier",
            "name": "E"
          },
          {
            "type": "Identifier",
            "name": "B"
          }
      ]
    }
  }
}

and I`m trying this code for my purpose and in this first I check value of JProperties and if there was something starts with '[' I sub String it and parse it again other otherwise print that :(r is my Json string)

JObject a = JObject.Parse(r);
            ArrayList ab= new ArrayList();
            String reg=@"{[-a-zA-Z0-9_~,'"":\s]*}\s*,\s*{[-a-zA-Z0-9~,'"":\s]*}";
            ab.Add(a);
                for (int i = 0; i < ab.Count; i++ )
                {
                    JObject d=ab[i] as JObject;
                    foreach (JProperty p in (d.Children()))
                    {
                        String val = p.Value.ToString();
                        if( val.StartsWith("[")){
                            val=val.Substring(2,val.Length-2);
                            if(Regex.Match(val,reg).Success)
                            {
                                String reg2 = @"},\s*{";
                                 int num=Regex.Match(val,reg2).Index;
                                 String val1 = val.Substring(0,num+1);
                                 JObject newob = JObject.Parse(val1);
                                 ab.Add(newob);
                                 val = val.Substring(num+3);
                                 newob = JObject.Parse(val);
                                 ab.Add(newob);
                            }
                            if (!val.Equals("")) { 
                            JObject newob=JObject.Parse(val);
                            ab.Add(newob);}
                        }
                        else if (val.StartsWith("{"))
                        {
                            if (!val.Equals(""))
                            {
                                JObject newob = JObject.Parse(val);
                                ab.Add(newob);
                            }
                        }
                        else
                        {
                            Console.WriteLine("value is: {0}", p.Value);
                        }
                  }
                }

but there is always error for sub String....they always are incorrect! could anyone help me? or offer me new way?

note:I don`t know the JSON string and it is every time different

  • Which line is causing you trouble? Do you get an error? – Simon Oct 23 '14 at 11:13
  • I receive an error: (Unexpected end of content while loading JObject. Path 'expression.callee.params[0]', line 13, position 12.) at line " JObject newob = JObject.Parse(val1);" – S. Esmaili Oct 23 '14 at 11:20
  • I understand this error because in val1 I see two } missing at the end of string...but I don`t know why! – S. Esmaili Oct 23 '14 at 11:22
  • What problem are you really trying to solve here? What is the overall purpose of your code? – Brian Rogers Oct 23 '14 at 14:06
  • I want to find some specific string in the value part of this json and I want to know the path, parent and child of what I found. For that in first place I want to print parent and child but I can`t. – S. Esmaili Oct 23 '14 at 14:42
  • In that case, [this question](http://stackoverflow.com/q/19645501/10263) might help you. – Brian Rogers Oct 23 '14 at 15:50

0 Answers0