0

I have a JSON string as following, when I parse this with JObject.parse, I have one exception because there is a root at the beginning, so now I want to remove the root in this case, I have searched some examples on Internet but there is none using JObject.parse.

var data = JObject.Parse(content)

setting.callbacks._2({"listDatesDebut":["2014-06-20"],"listDatesFin":["2014-06-21","2014-06-22","2014-06-23","2014-06-24","2014-06-25","2014-06-26","2014-06-27","2014-06-28","2014-06-29","2014-06-30","2014-07-01","2014-07-02","2014-07-03","2014-07-04","2014-07-05","2014-07-06","2014-07-07","2014-07-08","2014-07-09","2014-07-10","2014-07-11","2014-07-12","2014-07-14","2014-07-15","2014-07-16","2014-07-17","2014-07-18"],"listCategoriesThemes":[{"idCategorieTheme":1,"nomCategorieTheme":"123 double"},{"idCategorieTheme":3,"nomCategorieTheme":"PMR"},{"idCategorieTheme":2,"nomCategorieTheme":"2 lits simples"}],"listThemes":[{"idTheme":1,"nomTheme":"STANDARD CAR" .... )

In this example I want to remove the root setting.callbacks._2( and the last closing bracket of the root

Brian Rogers
  • 125,747
  • 31
  • 299
  • 300
TrangZinita
  • 107
  • 1
  • 9
  • The best way to do this is to alter the string, eliminating anything that isn't one of these three characters `{`, `(` or `"`. – Jonathan M Jun 06 '14 at 14:16
  • possible duplicate of [How to parse JSONP using JSON.NET?](http://stackoverflow.com/questions/12362456/how-to-parse-jsonp-using-json-net) – lucuma Jun 06 '14 at 14:17
  • @lucuma, This one has nothing to do with URL-encoded stuff in the json string. He's wanting to strip off the function name at the beginning and the surrounding parens. – Jonathan M Jun 06 '14 at 14:19
  • Good point although it answers the question. I removed my vote. – lucuma Jun 06 '14 at 14:20
  • @lucuma. Oh, I was assuming he doesn't know the name of the callback function. – Jonathan M Jun 06 '14 at 14:21
  • @TrangZinita, do you always know the name of the callback function, or is it unknown to your code? – Jonathan M Jun 06 '14 at 14:22
  • @JonathanM : it is just a name of a root, and I just wanted to remove it before reading Thanks – TrangZinita Jun 06 '14 at 14:28

2 Answers2

0

you can use Regex

var json = Regex.Match(jsonp, @".+\((.+)\).+").Groups[1].Value;

EDIT

BTW, if your are using this API, just remove the callback parameter from your url, then you will get a clean json.

L.B
  • 114,136
  • 19
  • 178
  • 224
  • Thanks but it doesnt work, as I just wanted to remove the setting.callbacks._2( the name of the root at the beginning and keep all the rest – TrangZinita Jun 06 '14 at 14:31
  • @TrangZinita *Doesn't work* doesn't mean anything. Do you get an error? not the the result you expected? What is the *full* input you use? I tested above code with the string in question and it *does* work. – L.B Jun 06 '14 at 14:34
  • Yes, when I tried it , it take out one string within the Json string and ignore all the rest, it is one string in the json which is too long that I can paste it all here. at first it is a string and setting.callbacks._2( is at beginning and one closing bracket is at the end, the middle is json value, and that is what I wish to keep – TrangZinita Jun 06 '14 at 14:36
0

Since you know that what you want is between the first and last parenthesis in the input string, you could just find the indices of those and take what is between:

int i = input.IndexOf('(');
int j = input.LastIndexOf(')');
string json = input.Substring(i + 1, j - i - 1);
JObject jo = JObject.Parse(json);
Brian Rogers
  • 125,747
  • 31
  • 299
  • 300