Click here to Skip to main content
15,887,175 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a JSON-String which looks like this :
{"wifi_sta":{"connected":true
"mqtt":{"connected":false}
"actions_stats":{"skipped":0}
"relays":[{"ison":false
"has_timer":false
"is_valid":true
{"ison":false
"has_timer":false
"is_valid":true}]
}

basicly I know how to deserialize JSON-Strings and assign them to a class - but now and here I have a class in a class ... AND ... the identifiers a equal.
The main-identifier is "relays" and the both sub-identifiers are the both "ison".
So ... there is my problem and I don't know how the class-object should look like ...

I'm very interested in Suggestions ...

What I have tried:

I created a sub-class with an array in it (ison(1) of boolean) - but that isn't assigned.
I created a structure with an array in it (ison() of boolean) - but that isn't assigned.

I also found this Article - but I don't understood it :
Deserializing Json Streams using Newtonsoft.Json & System.Text.Json with C# & VB[^]
Posted
Updated 15-Nov-23 2:29am
v3

1 solution

Well, for a start, that "JSON" is invalid. At a guess, it should look more like this:
JSON
{
    "wifi_sta": {
        "connected":true,
        "mqtt":{"connected":false},
        "actions_stats":{"skipped":0},
        "relays":[
            {
                "ison":false,
                "has_timer":false,
                "is_valid":true
            },
            {
                "ison":false,
                "has_timer":false,
                "is_valid":true
            }
        ]
    }
}
Once you have valid JSON, you can use Visual Studio's "paste JSON as classes" option to generate the classes you need to deserialize into.

Or you can use an online tool like quicktype[^] to generate the classes for you - although that one doesn't support VB.NET, but translating from C# shouldn't be too hard.
C#
public class Root
{
    [JsonPropertyName("wifi_sta")]
    public WifiSta WifiSta { get; set; }
}

public partial class WifiSta
{
    [JsonPropertyName("connected")]
    public bool Connected { get; set; }

    [JsonPropertyName("mqtt")]
    public Mqtt Mqtt { get; set; }

    [JsonPropertyName("actions_stats")]
    public ActionsStats ActionsStats { get; set; }

    [JsonPropertyName("relays")]
    public Relay[] Relays { get; set; }
}

public partial class ActionsStats
{
    [JsonPropertyName("skipped")]
    public long Skipped { get; set; }
}

public partial class Mqtt
{
    [JsonPropertyName("connected")]
    public bool Connected { get; set; }
}

public partial class Relay
{
    [JsonPropertyName("ison")]
    public bool Ison { get; set; }

    [JsonPropertyName("has_timer")]
    public bool HasTimer { get; set; }

    [JsonPropertyName("is_valid")]
    public bool IsValid { get; set; }
}
 
Share this answer
 
Comments
CPallini 15-Nov-23 8:47am    
5.
Ralf Meier 15-Nov-23 8:59am    
Thank you very very much, Richard. My +5
Of course, you posted in C# - but that wasn't a problem for me (I only prefer coding in VB)
The de-serialisation is working now ... 👍

With the original posted JSON from me you are right - I had formatted it for better seeing without the commas (replace "," with vbCRLF)
Ralf Meier 15-Nov-23 11:09am    
Also an additional a very good Sugestion from you was the PropertyName-Deklaration - some of the Properties have a Name which is not useful to be worked with the Deserializer - mean or example "input:0{sub-deklarations}" - here I could give a different Propertyname for the Deklaration and for the Deserializer ...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900