1

I have following string.

string jsonString = "[['Angelica','Ramos'],['Ashton','Cox']]";

I want to parse it into Javascript array like

[
    [
        "Angelica",
        "Ramos"
    ],
    [
        "Ashton",
        "Cox"
    ]
]

similar to Json.parse command. Any idea how to do this with .NET?

I tried both

JsonConvert.DeserializeObject(jsonString)

and

JsonConvert.DeserializeObject<List<object>>(jsonString)

but no luck so far

UPDATE ANSWER :

Oluwafemi answer is correct, here's the example between Oluwafemi answer and WHOl

enter image description here

tickwave
  • 3,335
  • 6
  • 41
  • 82
  • 1
    You *can't* parse it "into a JavaScript array", because you're not using JavaScript.. also the "string" shown is JavaScript (or broken C#), *not* JSON. A finally, the problem description - "no luck so far" - is non-existent. – user2864740 Sep 14 '15 at 08:53
  • Your jsonString is an array, not a string. You could try http://www.newtonsoft.com/json. Or maybe pass it in as an object, instead of an array. So put 'jsonObject = ' at the beginning. – MrFox Sep 14 '15 at 08:57
  • did you mean var list = JsonConvert.DeserializeObject>>(json); ? – Sagi Sep 14 '15 at 08:57
  • @user2864740: I believe it's a valid JSON because datatables plugin expect this format. – tickwave Sep 14 '15 at 09:20
  • @warheat1990 It is still not valid [JSON](http://www.json.org/) - nor does the string literal inside the "following string" represent valid JSON. – user2864740 Sep 14 '15 at 09:21
  • @user2864740: why not? You can copy the string and paste it to any online JSON converter and it will recognize is as a valid JSON. – tickwave Sep 14 '15 at 09:24
  • Please visit the link provided to the JSON grammar rules and/or use a JSON linter. But enough [trying to correct all the wrong on the internet](https://xkcd.com/386/) .. – user2864740 Sep 14 '15 at 09:25
  • @user2864740: I don't mean to be a jackass, but what am I suppose to look in that page? Is it because my Json start with square bracket? I'm not trying to correct anything, just honest question, it seems like you're getting salty though http://stackoverflow.com/questions/5034444/can-a-json-start-with – tickwave Sep 14 '15 at 09:32
  • The railroad tracks - for string :) The *only* valid string (or key, and which is required to be quoted) quote in JSON is a double quote. The RFC JSON updates allow any JSON value to be the root value. – user2864740 Sep 14 '15 at 09:33

2 Answers2

2

This is a 2 dimension array and to get this to work you need to do it this way:

string result = @"[['Angelica','Ramos'],['Ashton','Cox']]";
string[][] arr = JsonConvert.DeserializeObject<string[][]>(result);
Oluwafemi
  • 14,243
  • 11
  • 43
  • 59
1

Add this

using Newtonsoft.Json.Linq;

Try this

string jsonString = "[[\"Angelica\",\"Ramos\"],[\"Ashton\",\"Cox\"]]";
JArray ja = JsonConvert.DeserializeObject<JArray>(jsonString);

http://www.newtonsoft.com/json/help/html/T_Newtonsoft_Json_Linq_JArray.htm

WHol
  • 2,206
  • 2
  • 13
  • 16