-3

I'm currently working on a project which requires me to deserialize a JSON file, but it's showing to be harder to do and understand than I initially thought it would be.

What I want to do is deserialize this JSON file into a ListBox on my Windows Form project (located here). I want to put each of the versions located in "Unity5Stable" into the ListBox (5.4.3, 5.4.2, etc.)

Partial json:

{
  "Unity5Stable": {
    "5.4.3": {
      "x86": "http://netstorage.unity3d.com/unity/01f4c123905a/Windows32EditorInstaller/UnitySetup32-5.4.3f1.exe",
      "x64": "http://netstorage.unity3d.com/unity/01f4c123905a/Windows64EditorInstaller/UnitySetup64-5.4.3f1.exe"
    },
    "5.4.2": {
      "x86": "http://download.unity3d.com/download_unity/b7e030c65c9b/Windows32EditorInstaller/UnitySetup32-5.4.2f2.exe",
      "x64": "http://download.unity3d.com/download_unity/b7e030c65c9b/Windows64EditorInstaller/UnitySetup64-5.4.2f2.exe"
    },
    "5.4.1": {
      "x86": "http://download.unity3d.com/download_unity/649f48bbbf0f/Windows32EditorInstaller/UnitySetup32-5.4.1f1.exe",
      "x64": "http://download.unity3d.com/download_unity/649f48bbbf0f/Windows64EditorInstaller/UnitySetup64-5.4.1f1.exe"
    }
  }
}

I've tried a few different suggestions from a couple sites but I'm having a hard time finding anything related to putting the objects into a ListBox.

Any help would be greatly appreciated!

HebeleHododo
  • 3,620
  • 1
  • 29
  • 38
  • you could start by adding a `ListBox` to your form... and as your title in github nicly states "Because I hate doing things myself".. try harder or search anybody else – NtFreX Nov 27 '16 at 23:54
  • 1
    Not entirely sure why you're being hostile, I have a listbox ready, and some broken code from me trying it myself in a local repo. I'm not pushing unfinished code to the online repo. As I also stated, I've tried searching, and have been working on this locally for around six hours now. I apologize if I worded the question wrong and gave that impression, though. – Duck Bread Nov 28 '16 at 00:10
  • @DuckBread Welcome to SO. I suggest to go trough [Help Center > Asking](http://stackoverflow.com/help/asking) to understand what is a good question and what is not. Also don't link to offsite resources, but instead write the necessary details withing the question itself. If the offsite resources is offline for any reason the question itself here is not usable for other readers. – Jim Nov 28 '16 at 02:19
  • @DuckBread can you change the json file? It would be much easier for you if you could make Unity5Stable a dictionary. – HebeleHododo Nov 28 '16 at 14:32

2 Answers2

0

Im not familiar with listboxes but I do know you can deserialize a json into an object. You create an object with the same type and name variables your json has, deserialize into it and then you can access your variables. I used newtonsoft.JSON in a xamarin forms project.

fstam
  • 669
  • 4
  • 20
0

JSON Deserialization

You can deserialize JSON to an known type with the JsonSerializer from Newtonsoft.Json (More details here)

Or with the Json class from System.Web.Helpers you can deserialize JSON to an dynamic. (More details here)

dynamic data = Json.Decode(json);

ListBox

Your ListBox has an property Items which is an collection where you can add objects. You can add a string or any other object. The ToString method of the object will be called and it's value will be displayed.

Community
  • 1
  • 1
NtFreX
  • 10,379
  • 2
  • 43
  • 63
  • I think I'm on the right track now - I noticed it's going to be impossible to do this correctly due to the JSON object names I've used. You can't use decimals (heck, even numbers) for method names. Is there a way around that? – Duck Bread Nov 28 '16 at 00:48
  • @DuckBread yes your right thats a problem. in generel you can use numbers in variable names but not at the start of them. But its usualy a bad practice to do so as is makes the code unreadable. – NtFreX Nov 28 '16 at 00:51
  • I have it setup to where it'll accept data into the ListBox from the JSON, only now I'm getting an "Unexpected character" exception. I've created a new branch for the issue on the [GitHub repo here](https://github.com/ryanguy426/UVM/tree/Broken-StackOverflow), and this is the error I'm getting now: http://pastebin.com/CLHJCLaB. I've also moved the problem code into [MainWindow.cs](https://github.com/ryanguy426/UVM/blob/Broken-StackOverflow/UVM/MainWindow.cs) so it's slightly easier to reach. – Duck Bread Nov 28 '16 at 01:24
  • @DuckBread accept a post as answer which helped you for this problem or create your own and open a new question for the new problem – NtFreX Nov 28 '16 at 11:36