$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>How to parser JavaScript multidimensional array to c# array? - Stack Overflow|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

How to parser JavaScript multidimensional array to c# array? - Stack Overflow

matteradmin16PV0评论

Example of JavaScript arrays:

var array_1 = [["string 1", 2013, "string 2"], "string 3", ["string 4", , "string 5"]];
/* array_1[0][2] = "string 2" */

var array_2 = [1, , ["string 1", "string 2"]];
/* array_2[0][0] = 1 */

I need to parse JS arrays like it to c# jagged array or any other object that can access each child string by index easy, by function, with:

  • number bee string (1 => "1")
  • null bee "" (string with length = 0).

Can you help me how to do this? Thank you very much!

Example of JavaScript arrays:

var array_1 = [["string 1", 2013, "string 2"], "string 3", ["string 4", , "string 5"]];
/* array_1[0][2] = "string 2" */

var array_2 = [1, , ["string 1", "string 2"]];
/* array_2[0][0] = 1 */

I need to parse JS arrays like it to c# jagged array or any other object that can access each child string by index easy, by function, with:

  • number bee string (1 => "1")
  • null bee "" (string with length = 0).

Can you help me how to do this? Thank you very much!

Share Improve this question edited Apr 13, 2013 at 3:46 NoName asked Apr 10, 2013 at 5:51 NoNameNoName 8,03516 gold badges60 silver badges114 bronze badges 0
Add a ment  | 

5 Answers 5

Reset to default 4 +50

using Json.NET

// using
using Newtonsoft.Json.Linq;


string JSarray_1 = @"[[""string 1"", 2013, ""string 2""], ""string 3"", [""string 4"", , ""string 5""]]";
JObject j = JObject.Parse("{\"j\":" + JSarray_1 + "}");
MessageBox.Show((string)j["j"][0][2]); // "string 2"

See the C# language documentation: "Multidimensional Arrays (C#)"

string[,] items = new  string[,] {{"string 1","string 2"},...};

I think what TuyenTk is looking for, and emigue is trying to describe is to use a library which does the "magic"(=parsing)

I'd remend JSON.Net since it's the one I use all the time - but I guess there are plenty of these out there.

The linked page also includes some simple examples on how to use it.

About replacing null with emptystring:

var myValue = origValue ?? String.Empty;

if origValue is null myValue will be set to "", otherwise the expression will evaluate to origValue;

For further information on "??", or the "null-coalescing operator" as it's called, see the doc

As Jagged Arrays

string[][] items = new string[3][];

items [0] = new string[2];
items [1] = new string[1];
items [2] = new string[2];

items[0][0] = "string1";
items[0][1] = "string3";
items[1][0] = "string4";
items[2][0] = "string5";
items[2][1] = "string6";

OR

string[][] items = new string[][] 
{
    new string[] {"string1", "string3"},
    new string[] {"string4"},
    new string[] {"string5", "string6"}
};

If you need parse javascript arrays to c# arrays, You can serialize Javascript arrays to JSON and then deserialize JSON to C# array.

Previously, you need to do one transformation: replace "" by null in Javascript array representation as string.

Then, you can make something like this:

var JSArrayString = @"{"array_1": [["string 1", 2013, "string 2"], "string 3", ["string 4", null, "string 5"]]}";
var CSharpDict = SomeJSONLibrary.Deserialize(JSString);
var CSharpArray = CSharpDict["array_1"];
var myItem = CSharpArray[0][2];
Post a comment

comment list (0)

  1. No comments so far