0

I've been searching for an answer to this so sorry if it's been asked before, but... I'm converting a Javascript project to C#, which I'm trying to learn as I go, but I'm not sure how to convert a large Javascript array to something readable in C#. Here's an example line from the array:

var myArray=[
"6016-SH",90,0,{door:[304,35,65],key:[60,58,8,5]},
]

In Javascript I search for that "6016-SH" string with indexOf then access the subarray (myArray[3]) then look for and access various variables, i.e. myArray[3].door[1] etc. So is there any quick way of converting this to C# without reformatting the whole thing? I'm presuming a list would be the best option?

Thanks.

Crusty
  • 1
  • 1
  • 9
    even in JavaScript parsing a JSON using string-operations is rarely a good idea and causes just headache. Use a Json-parser instead, for C# the best would be NewtonSoft`. – MakePeaceGreatAgain Mar 16 '21 at 09:52
  • What you have is a `JavaScript Object`. It's not a simple string it's the serialization of an object, a "textual picture". You can parse it like if it was JSON using [newtownsoft](https://www.newtonsoft.com/json/help/html/DeserializeObject.htm). – Self Mar 16 '21 at 10:04
  • We will need the full object declaration in order to know the calss definition. But you can simply get the json from that Javascript object by using one line of Js `JSON.stringify(obj)` like that https://www.w3schools.com/js/tryit.asp?filename=tryjson_stringify . Then Once you have a json you can easy create the class representing the object with only one copy past https://stackoverflow.com/questions/21611674/how-to-auto-generate-a-c-sharp-class-file-from-a-json-string. – Self Mar 16 '21 at 10:04
  • Thanks for that - I can get the JSON from each object but am I right thinking I'm still going to have to extract each line manually, i.e. chopping off the string and values before the object? At the moment the whole thing is just wrapped in a var = [ ] in Javascript and there are around 6000+ lines. – Crusty Mar 16 '21 at 10:25
  • It doesn't look like "array" was the best choice of javascript datatype here - I think it should have been an object. Even more so for C#: an array containing strings, integers and objects is cause for headaches. – Hans Kesting Mar 16 '21 at 10:47
  • Ha! Yes I know. I'm thinking I'll just convert this line by line into something more C# friendly. – Crusty Mar 16 '21 at 10:50
  • May you [edit] a more representative version of the Json ? you speak of "_search for that "6016-SH" string with indexOf then access the subarray (myArray[3])_". So I expect the array to look like `[{"6016-SH",90,0,{"door":[304,35,65],"key":[60,58,8,5]}}, {"Foo-bar",90,0,{"door":[304,35,65],"key":[60,58,8,5]}}]` an array of some object – Self Mar 16 '21 at 13:22
  • I will do something like that https://dotnetfiddle.net/0KLxs2. But probably wrong mappin if the structure change – Self Mar 16 '21 at 13:43
  • Thanks Self - I'll stare at that code until I understand it! – Crusty Mar 16 '21 at 18:05

0 Answers0