0

I'm trying to convert a JSON object from JavaScript into C#. I want to be able to access it like you would in normal JavaScript, like this: letters.property[arrayindex].

I tried some of those online converters, which gave me the class, but some of the properties of the object are invalid; I basically have one property for every letter of the alphabet, and some of the punctuation characters, like the semicolon, which C# won't let me use as an object property.

Here is the JSON object I want to convert:

 {
            "-": [

        ["0", "0", "0", "0", "0", "0", "0", "0"],
        ["0", "0", "0", "0", "0", "0", "0", "0"],
        ["0", "0", "1", "1", "1", "1", "0", "0"],
        ["0", "0", "1", "1", "1", "1", "0", "0"],
        ["0", "0", "1", "1", "1", "1", "0", "0"],
        ["0", "0", "0", "0", "0", "0", "0", "0"],
        ["0", "0", "0", "0", "0", "0", "0", "0"],
        ["0", "0", "0", "0", "0", "0", "0", "0"]



   ],
}

That is just one property of the object, there are many after that as well that are similar to this one. How do I convert this?

1 Answers1

3

You could try to deserialize to something like this:

var charSet = JsonConvert.DeserializeObject<Dictionary<char, int[][]>>(json);
var valueSet = charSet['-']; 
JSteward
  • 6,833
  • 2
  • 21
  • 30
  • So if I do JArray.Parse(String), will that fill in for the json in the code? – Bop It Freak Jan 28 '19 at 20:25
  • For `json` I used what you had posted in the OP, minus the trailing comma. So it should work for you. Just use the raw string not `JArray.Parse` – JSteward Jan 28 '19 at 20:27
  • so I did `Dictionary charSet = JsonConvert.DeserializeObject>(json); Bot.letters = charSet;` and Bot.letters is also a `Dictionary`, but now when i try to access it like this: `letters["A"]`, it says it can't convert string to char. – Bop It Freak Jan 28 '19 at 21:07
  • 1
    Char has single quotes, 'A' – JSteward Jan 28 '19 at 21:08