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

9
  • 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. Commented Sep 14, 2015 at 8:53
  • Your jsonString is an array, not a string. You could try newtonsoft.com/json. Or maybe pass it in as an object, instead of an array. So put 'jsonObject = ' at the beginning. Commented Sep 14, 2015 at 8:57
  • did you mean var list = JsonConvert.DeserializeObject<List<List<object>>>(json); ? Commented Sep 14, 2015 at 8:57
  • @user2864740: I believe it's a valid JSON because datatables plugin expect this format. Commented Sep 14, 2015 at 9:20
  • @warheat1990 It is still not valid JSON - nor does the string literal inside the "following string" represent valid JSON. Commented Sep 14, 2015 at 9:21

2 Answers 2

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);
Sign up to request clarification or add additional context in comments.

5 Comments

works thanks! please see my updated post to compare between your answer and WHol
@user2864740 I have just updated my answer with a valid json.
Still wrong on the replace.. and without the dubious text replace it is still invalid JSON. JSON is a very specific format. Anything else is .. something else, leniency in parsing aside.
The problem with the original string literal is @"foo\bar" == "foo\\bar". The slash has no meaning in a verbatim string literal.
I see. Thanks for the heads up!
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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.