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?

7

1 Answer 1

3

You could try to deserialize to something like this:

var charSet = JsonConvert.DeserializeObject<Dictionary<char, int[][]>>(json);
var valueSet = charSet['-']; 
Sign up to request clarification or add additional context in comments.

4 Comments

So if I do JArray.Parse(String), will that fill in for the json in the code?
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
so I did Dictionary<char, int[][]> charSet = JsonConvert.DeserializeObject<Dictionary<char, int[][]>>(json); Bot.letters = charSet; and Bot.letters is also a Dictionary<char, int[][]>, but now when i try to access it like this: letters["A"], it says it can't convert string to char.
Char has single quotes, 'A'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.