C
C#

help

✅ JsonConvert debugging help

Mmarsastro2/3/2023
Hello! I'm having a hard time figuring out how to solve an issue where JsonConvert.DeserializeObject is struggling to parse a weird data structure. Essentially I'm getting an object like this: {"powerup":{"on":{"on":{"on":true}}}} There are more objects and values, but I'm omitting them as they're not relevant to the parsing error In other words, an object called powerup with an object called on, that has an object called on, that has a bool called on. Very odd data structure, I know, but unfortunately this is what the API I'm consuming is giving me. A json converter tool suggested this:
public class Powerup
{
public On on { get; set; }
}

public class On
{
public bool on { get; set; }
}
public class Powerup
{
public On on { get; set; }
}

public class On
{
public bool on { get; set; }
}
However, this reads to a parsing error saying that { after the second on objects : is an unexpected character. Which makes sense, as it's expecting a bool, not an object. So I tried to fix it by doing this:
public class Powerup
{
public On on { get; set; }
}

public class On
{
public On2 on { get; set; }
}

public class On2
{
public bool on { get; set; }
}
public class Powerup
{
public On on { get; set; }
}

public class On
{
public On2 on { get; set; }
}

public class On2
{
public bool on { get; set; }
}
However, now I'm getting the error that it fails to convert a boolean value to the type On2, which seems to suggest that it's now suddenly reading it correctly as a bool and trying to set the On2 on variable with this bool value. I'm very confused at this point. Anyone have any idea what's going on here?
AAngius2/3/2023
quictype.io generated the following:
namespace Parser
{
public class Root
{
[JsonPropertyName("powerup")]
public Powerup Powerup { get; set; }
}

public class Powerup
{
[JsonPropertyName("on")]
public PowerupOn On { get; set; }
}

public class PowerupOn
{
[JsonPropertyName("on")]
public OnOn On { get; set; }
}

public class OnOn
{
[JsonPropertyName("on")]
public bool On { get; set; }
}
}
namespace Parser
{
public class Root
{
[JsonPropertyName("powerup")]
public Powerup Powerup { get; set; }
}

public class Powerup
{
[JsonPropertyName("on")]
public PowerupOn On { get; set; }
}

public class PowerupOn
{
[JsonPropertyName("on")]
public OnOn On { get; set; }
}

public class OnOn
{
[JsonPropertyName("on")]
public bool On { get; set; }
}
}
see if it works Although it was generated for System.Text.Json, and you seem to be using Newtonsoft for some reason Just change JsonPropertyName to JsonProperty and it'll work with Newtonsoft
Mmarsastro2/3/2023
Gonna test it now, but that does seem very promising Got this: "Error converting value True to type 'namespace.OnOn'" Seems to be the same error as when I tried On2
MMODiX2/3/2023
Angius#1586
REPL Result: Success
using System;
using System.Collections.Generic;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Globalization;

public partial class Root
{
[JsonPropertyName("powerup")]
public Powerup Powerup { get; set; }
}
public partial class Powerup
{
[JsonPropertyName("on")]
public PowerupOn On { get; set; }
}
public partial class PowerupOn
{
[JsonPropertyName("on")]
public OnOn On { get; set; }
}
public partial class OnOn
{
[JsonPropertyName("on")]
public bool On { get; set; }
}

var json = """
{
"powerup":
{
"on":
{
"on":
{
"on": true
}
}
}
}
""";

var data = JsonSerializer.Deserialize<Root>(json);
data
using System;
using System.Collections.Generic;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Globalization;

public partial class Root
{
[JsonPropertyName("powerup")]
public Powerup Powerup { get; set; }
}
public partial class Powerup
{
[JsonPropertyName("on")]
public PowerupOn On { get; set; }
}
public partial class PowerupOn
{
[JsonPropertyName("on")]
public OnOn On { get; set; }
}
public partial class OnOn
{
[JsonPropertyName("on")]
public bool On { get; set; }
}

var json = """
{
"powerup":
{
"on":
{
"on":
{
"on": true
}
}
}
}
""";

var data = JsonSerializer.Deserialize<Root>(json);
data
Result: Root
{
"powerup": {
"on": {
"on": {
"on": true
}
}
}
}
{
"powerup": {
"on": {
"on": {
"on": true
}
}
}
}
Compile: 578.397ms | Execution: 60.214ms | React with ❌ to remove this embed.
AAngius2/3/2023
Seems to be working At least with STJ
Mmarsastro2/3/2023
Hmm, maybe I'll give it a shot with STJ instead then. Didn't pick newtonsoft for any particular reason, it was just the first option I found
UUUnknown User2/3/2023
2 Messages Not Public
Sign In & Join Server To View
MMODiX2/3/2023
Angius#1586
Just change JsonPropertyName to JsonProperty and it'll work with Newtonsoft
Quoted by
<@!85903769203642368> from #JsonConvert debugging help (click here)
React with ❌ to remove this embed.
UUUnknown User2/3/2023
2 Messages Not Public
Sign In & Join Server To View
Mmarsastro2/3/2023
There, finally got it to work. Ended up with some new issues, but that was just quicktype interpreting float values as long. STJ did the trick I'll make sure I avoid newtonsoft in the future, thanks for the help!

Looking for more? Join the community!

Want results from more Discord servers?
Add your server
Recommended Posts
❔ Make a sprite follow mouse while pivoting around character in UnityHi, I its hard to describe my issue by describing it since im not sure exactly what I'm looking for ❔ is Generic Repository a bad implementation of Repository Pattern?Surfing the internet I have found several articles that say it is an anti-pattern because it is an a❔ Problems with using Dotnet 6 on Windows to publish to an exe formatDotnet/C# newb here. Previously I've only done a small amount of development using the **.Net Framew❔ Using net7 documentation while targeting netstandard2.0This might be a bit of a dumb question, but is there anyway to make omnisharp/roslyn's tooltips use ❔ WPF app unit testingI am learning how to make unit tests. I built my app on .net core 6.0 and I created class library (o❔ where exactly should i put authorizationso i read a few posts on where to put authorization and most of the people agree on putting it in th❔ C# WPF playing background musicHello, I have WPF app for game launcher/updater. I wanted to add background music, so I added this cC# wpf mvvm Datagrid filter using LINQSo here I have a MVVM form. the Form contains a Datagrid which is connected to the Databank. I also ❔ Is it worth to create lazy dictionary in this case?I have following class: so as you see Add function add to the dictionary class TreeNode, it looks li❔ Going to use a listbox with a switch for something in the morning, need help with format for bothI would like to set two variables with a listbox using a switch and one variable with a second listb✅ What's your view on tool like Sonarqube?Should I learn from the code smells it captures?✅ Autodesk Maya HelpAnyone in here know Autodesk Maya really well? My mesh tool isn't letting my polygon primitives mesh❔ circular primesso this program is meant to find how many circular primes there are below 1 million (should be 55), ❔ WPF Cannot find child when it's thereHi! I am working on my own RoundedCorners attachedproperty however I have stumbled upon a problem Pr❔ HttpClient exiting program during GetStringAsync() and GetAsync()I'm sending a GET to an API, but when debugging, as soon as you step off the last line, it cuts out ✅ Homework Helphttps://i.gyazo.com/f23d758d46581bcfb84f88d4178cd484.png What should I return from the FillArray() ❔ force a ushort value?if i type for example var num = 25L; i am forcing a long value (int64) how can i force a ushort in ❔ Doing post method with entity frameworki'm making a simple API to practice with entity framework but when i try to make a post to a table w❔ I can't reference a class library please helpI can't reference a class library i t says compilation error❔ ASP.NET REMOTE DEVELOPMENTHey does anyone know if I would need nginx for remote development on a ubuntu server from my window