1

I'm pretty new to C#, i have this code:

downloads obj = JsonConvert.DeserializeObject<downloads>(data);

foreach (KeyValuePair<string, client> kvp in obj.client)
{
    Console.WriteLine("URL: " + kvp.Value.url);
    Console.WriteLine("SHA1: " + kvp.Value.sha1);
    matrix[0].Add(kvp.Value.url);
    matrix[0].Add(kvp.Value.sha1);
}

with these classes:

class downloads
{
    [JsonProperty("client")]
    public Dictionary<string, client> client { get; set; }
}

class client
{
    [JsonProperty("url")]
    public string url { get; set; }
    [JsonProperty("sha1")]
    public string sha1 { get; set; }
}

It says me Object reference not set to an instance of an object but how can i create a reference and use that reference as a type? I mean, i create a reference to downloads this say:

downloads down = new downloads();

but now how can i assign a variable that json deserialize? here is the complete code: https://dotnetfiddle.net/F9mSvQ

Partial JSON:

{ 
"downloads": 
  {
    "client": { "sha1": "e80d9b3bf5085002218d4be59e668bac718abbc6" },
    "server": { "sha1": "952438ac4e01b4d115c5fc38f891710c4941df29" }
   }
 }
Alexei Levenkov
  • 94,391
  • 12
  • 114
  • 159
giogiowefj
  • 103
  • 7
  • 2
    From what I see on your fiddle, you have a wrongly formatted json string. – DPac Feb 10 '16 at 16:38
  • 1
    If the structure of the JSON isn't actually a serialized version of the type that you're building, then the de-serialization will probably just return `null`. – David Feb 10 '16 at 16:40
  • 1
    @DPac Actually it's fine (add a `Console.Write` for it and run it through jsonlint.com) - it just doesn't match the type being deserialised to. – James Thorpe Feb 10 '16 at 16:41
  • @JamesThorpe That is what I meant. The json structure doesn't match `downloads` structure. – DPac Feb 10 '16 at 16:41
  • ok there was a braket more. But that's not the problem, the error i get is Object reference not set to an instance of an object. I know what that error means but i don't know how to solve it in this specific case – giogiowefj Feb 10 '16 at 16:42
  • sorry didn't see your posts – giogiowefj Feb 10 '16 at 16:42
  • why doesn't it match the downloads structure? – giogiowefj Feb 10 '16 at 16:43
  • The `downloads` part of it matches, but it has additional properties that don't exist. If you want a helping hand, run your json through http://json2csharp.com/ - it will generate classes for everything in the json, then you can deserialise it all to the root. – James Thorpe Feb 10 '16 at 16:44
  • i tried to run it on json2csharp.com and i implemented it in my code but now? – giogiowefj Feb 10 '16 at 16:51
  • @giogiowefj Added my answer below – DPac Feb 10 '16 at 17:11

2 Answers2

1

From the JSON downloads itself should be dictionary on Client objects OR contain several properties of type Client like

class Downloads
{
  public Client client { get; set; }
  public Client server { get; set; }
}

Debugging note: It is always good idea to serialize your class to see what result it produces - this would let you know if structure looks matching or not.

Alexei Levenkov
  • 94,391
  • 12
  • 114
  • 159
  • i added it in the code but it says foreach cannot operate on variables Program.Client because they have no getenumerator definition – giogiowefj Feb 10 '16 at 16:57
  • @giogiowefj - it is up to you to figure out which approach to take to read the JSON you have - classes that you have now don't match JSON and your iteration code will need to be changed. Picking implementation approach is generally outside of scope of SO answers which usually just suggest approaches. – Alexei Levenkov Feb 10 '16 at 17:02
0

First of all, I suggest you to stick to traditional Naming Conventions

It will help you and others to see your code better and more clear.

Based on the class structure you have, here is the json format that you want.

string json = @"{clients: {
     'clientname1':{'sha1':'shahashhere','url':'urlhere'},
     'clientname2':{'sha1':'shahashhere','url':'urlhere'}
 }}";

Change the values as needed.

Here is link to the https://dotnetfiddle.net/uedQ8i. I changed the classes name as I was confused while I was working on it.

EDIT

Since OP doesn't have control of JSON string, then another class have to be made to contain downloads object

class MinecraftObject {
    Downloads downloads {get;set;}
}

Deserializing the json string

var obj = JsonConvert.DeserializeObject<MinecraftObject>(json);
var download = obj.downloads;
DPac
  • 515
  • 2
  • 8
  • the json structure is not like that, i get the file from here: https://s3.amazonaws.com/Minecraft.Download/versions/1.7.10/1.7.10.json. I can't apply your code to that json – giogiowefj Feb 10 '16 at 17:28