https request which is a bool but it also isnt?

okay so my code is pretty long but ill sum it up real quick, so pretty much im making a https request in which i get this json back "isJunior":false now i want to compare this false statment with the rest of my code but its making it harder than what it should be
False
Error while scraping: Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: Cannot implicitly convert type 'Newtonsoft.Json.Linq.JValue' to 'bool'. An explicit conversion exists (are you missing a cast?)
at CallSite.Target(Closure, CallSite, Object)
at AE.Scraper.<>c__DisplayClass3_0.<<StartScrapingAsync>g__ScrapeThread|0>d.MoveNext() in C:\Users\t\Documents\Programming\C#\a\Program.cs:line 489
False
Error while scraping: Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: Cannot implicitly convert type 'Newtonsoft.Json.Linq.JValue' to 'bool'. An explicit conversion exists (are you missing a cast?)
at CallSite.Target(Closure, CallSite, Object)
at AE.Scraper.<>c__DisplayClass3_0.<<StartScrapingAsync>g__ScrapeThread|0>d.MoveNext() in C:\Users\t\Documents\Programming\C#\a\Program.cs:line 489
c#
Console.WriteLine($"{playerInfo["isJunior"]}");
bool checkJunior = false;
if (playerInfo["isJunior"].ToString() == "true") {
checkJunior = true;
} else {
checkJunior = false;
}
c#
Console.WriteLine($"{playerInfo["isJunior"]}");
bool checkJunior = false;
if (playerInfo["isJunior"].ToString() == "true") {
checkJunior = true;
} else {
checkJunior = false;
}
105 Replies
hoggy077
hoggy0772w ago
oof, newtonsoft
Nick Eh Likes 15
Nick Eh Likes 15OP2w ago
this has kind of done my head in now, im so confused on why it doesnt work
hoggy077
hoggy0772w ago
Are you serializing to an object? & maybe consider using System.Text.Json instead
Nick Eh Likes 15
Nick Eh Likes 15OP2w ago
im not actually sure, and ill have a look at the system.text.json but i dont think i am
hoggy077
hoggy0772w ago
we typically recommend it over Newtonsoft, the developer of Newtonsoft actually got hired & made STJ iirc if you can past the rest of the code that would be helpful
Nick Eh Likes 15
Nick Eh Likes 15OP2w ago
its pretty long, over 1k lines long and not even including the other files thats only the program file
hoggy077
hoggy0772w ago
$code
MODiX
MODiX2w ago
To post C# code type the following: ```cs // code here ``` Get an example by typing $codegif in chat For longer snippets, use: https://paste.mod.gg/
hoggy077
hoggy0772w ago
but I think JValue also allows you to cast directly to your type anyway, so if thats more apt for this if its just a short check, that would work as well ie. (bool)playerInfo["isJunior"]
Nick Eh Likes 15
Nick Eh Likes 15OP2w ago
BlazeBin - fagepsfzngtv
A tool for sharing your source code with the world!
Nick Eh Likes 15
Nick Eh Likes 15OP2w ago
i tried that False Error while scraping: Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: Cannot implicitly convert type 'Newtonsoft.Json.Linq.JValue' to 'bool'. An explicit conversion exists (are you missing a cast?) at CallSite.Target(Closure, CallSite, Object) at AE.Scraper.<>cDisplayClass3_0.<<StartScrapingAsync>gScrapeThread|0>d.MoveNext() in C:\Users\t\Documents\Programming\C#\a\Program.cs:line 489
Angius
Angius2w ago
Deserialize to a good and proper object Forget dynamic ever existed
hoggy077
hoggy0772w ago
^
Angius
Angius2w ago
Erase it from your mind It's not a thing It's fake
hoggy077
hoggy0772w ago
-# looks like Newtonsoft got some updates after the last time I used it Alternatively & probably more effective fo you, you can serialize to an object using LinQ, which will provide you what you want as a .Net
Angius
Angius2w ago
The universe is worse for it's existence
Nick Eh Likes 15
Nick Eh Likes 15OP2w ago
isnt that what im trying to do? if ((bool)playerInfo["isJunior"] == true) { checkJunior = true; } else { checkJunior = false; }
Angius
Angius2w ago
No You deserialize to dynamic
Nick Eh Likes 15
Nick Eh Likes 15OP2w ago
where should i deserializ it to
Angius
Angius2w ago
And to List<Dictionary<string, object>> To a good and proper object $jsongen might help
MODiX
MODiX2w ago
Use https://app.quicktype.io or https://json2csharp.com to generate classes from your JSON
Instantly parse JSON in any language | quicktype
Whether you're using C#, Swift, TypeScript, Go, C++ or other languages, quicktype generates models and helper code for quickly and safely reading JSON in your apps. Customize online with advanced options, or download a command-line tool.
Convert JSON to C# Classes Online - Json2CSharp Toolkit
Convert any JSON object to C# classes online. Json2CSharp is a free toolkit that will help you generate C# classes on the fly.
Angius
Angius2w ago
Make a proper class, deserialize to that
Nick Eh Likes 15
Nick Eh Likes 15OP2w ago
would that be using JsonConvert.DeserializeObject<PlayerInfo>(json); okay let me try
Angius
Angius2w ago
Ideally without Newtonsoft, but yeah JsonSerializer.Deserialize<PlayerInfo>(json) if we're writing code like it's 2025
hoggy077
hoggy0772w ago
is your isJunior a string by any chance?
Nick Eh Likes 15
Nick Eh Likes 15OP2w ago
"isJunior":false thats the json that comes back the isJunior it self is the value it holds isnt
hoggy077
hoggy0772w ago
ok, nvm then. That was the only reason I could think of casting failing to brazenly
Nick Eh Likes 15
Nick Eh Likes 15OP2w ago
wait but then how would i use that
Angius
Angius2w ago
Wym?
Nick Eh Likes 15
Nick Eh Likes 15OP2w ago
my json is from playerInfo["isJunior"] but what is <PlayerInfo>
Angius
Angius2w ago
A class that describes the JSON
hoggy077
hoggy0772w ago
that takes the Json & returns you the generic
Nick Eh Likes 15
Nick Eh Likes 15OP2w ago
so that can be anything?
Angius
Angius2w ago
A class that describes the JSON
Nick Eh Likes 15
Nick Eh Likes 15OP2w ago
okay yeah so if i do this i get a error within vs string info = JsonConvert.DeserializeObject<info>(playerInfo["isJunior"]); or am i mising something 'info' is a variable but is used like a typeCS0118 (local variable) string info Argument 1: cannot convert from 'object' to 'string'CS1503 class System.String Represents text as a sequence of UTF-16 code units
hoggy077
hoggy0772w ago
you feed it the original json string
MODiX
MODiX2w ago
Angius
REPL Result: Success
using System.Text.Json;

var json = """
{
"Foo": "bar",
"Count": 69,
"Bools": [ true, false, false, true ]
}
""";

class Unga
{
public string Foo { get; set; }
public int Count { get; set; }
public bool[] Bools { get; set; }
}

var data = JsonSerializer.Deserialize<Unga>(json);

var b = string.Join(", ", data.Bools);
Console.WriteLine($"Foo: {data.Foo}, Count: {data.Count}, Bools: {b}");
using System.Text.Json;

var json = """
{
"Foo": "bar",
"Count": 69,
"Bools": [ true, false, false, true ]
}
""";

class Unga
{
public string Foo { get; set; }
public int Count { get; set; }
public bool[] Bools { get; set; }
}

var data = JsonSerializer.Deserialize<Unga>(json);

var b = string.Join(", ", data.Bools);
Console.WriteLine($"Foo: {data.Foo}, Count: {data.Count}, Bools: {b}");
Console Output
Foo: bar, Count: 69, Bools: True, False, False, True
Foo: bar, Count: 69, Bools: True, False, False, True
Compile: 569.633ms | Execution: 62.087ms | React with ❌ to remove this embed.
Nick Eh Likes 15
Nick Eh Likes 15OP2w ago
but thats how i get it? oh right
Angius
Angius2w ago
No description
Angius
Angius2w ago
responseContent is the JSON you get from that request
hoggy077
hoggy0772w ago
Man, this is not the newtonsoft I remember
Angius
Angius2w ago
And higher up, this is also where you parse some JSON
No description
Angius
Angius2w ago
To a dynamic this time So burn it to the ground as well and use a proper class
hoggy077
hoggy0772w ago
definitely use a proper type
Nick Eh Likes 15
Nick Eh Likes 15OP2w ago
okay ill try my best but only been coding in c sharp for not very long
hoggy077
hoggy0772w ago
thats ok everyone starts somewhere
Nick Eh Likes 15
Nick Eh Likes 15OP2w ago
yeah exactly its a big change from something simple like python
hoggy077
hoggy0772w ago
hell, some of us have been doing C# for decades and are still learning
Angius
Angius2w ago
Now, most people probably don't start with a bunch of [DllImport]s and 700+ lines of code, but...
Nick Eh Likes 15
Nick Eh Likes 15OP2w ago
yeah i know this is a fork from something else but i would of defo added around 200-300 lines with no experience like i started c sharp a day or two ago just going with the flow and bit more than i can chew
hoggy077
hoggy0772w ago
forking a project a few days in, very bold
Nick Eh Likes 15
Nick Eh Likes 15OP2w ago
yep i mean i have knowleage from other projects in other langues but its like a why the hell not kind of move its fun okay so you see this code, after my class that im in "scraper" would i need to define this dict in my own way ofc, and then pass it right? because otherwise it wont be converted { public string Foo { get; set; } public int Count { get; set; } public bool[] Bools { get; set; } }
Angius
Angius2w ago
No dictionaries Json string -> deserializer -> class instance
Nick Eh Likes 15
Nick Eh Likes 15OP2w ago
what is this called then? { public string Foo { get; set; } public int Count { get; set; } public bool[] Bools { get; set; } }
Angius
Angius2w ago
No intermediary dictionaries, no nothing A class Well, you stripped away the class Unga So not a class in this case But, yeah, a class
Nick Eh Likes 15
Nick Eh Likes 15OP2w ago
so i have to add this after my class? { public string Foo { get; set; } public int Count { get; set; } public bool[] Bools { get; set; } } after scraper?
Angius
Angius2w ago
No description
Angius
Angius2w ago
This is a whole thing A class and its properties
Nick Eh Likes 15
Nick Eh Likes 15OP2w ago
oh i get it now
Angius
Angius2w ago
You can't just pick and choose bits and pieces from it
hoggy077
hoggy0772w ago
thats just an example
Angius
Angius2w ago
Yeah
Nick Eh Likes 15
Nick Eh Likes 15OP2w ago
so i need its own class?
hoggy077
hoggy0772w ago
yes
Angius
Angius2w ago
Yes One that describes your Json
Nick Eh Likes 15
Nick Eh Likes 15OP2w ago
but i can call from outside correct?
hoggy077
hoggy0772w ago
depends what you mean by outside
Nick Eh Likes 15
Nick Eh Likes 15OP2w ago
like just not in that class
hoggy077
hoggy0772w ago
outside the function? yes
Nick Eh Likes 15
Nick Eh Likes 15OP2w ago
outside the class
Angius
Angius2w ago
No description
Angius
Angius2w ago
Notice And, ideally, just place it in a different file
Nick Eh Likes 15
Nick Eh Likes 15OP2w ago
yes got it
Angius
Angius2w ago
Your code is already humongous as it is, you don't want to add another 50 lines for a class to deserialize to
Nick Eh Likes 15
Nick Eh Likes 15OP2w ago
thats true
Angius
Angius2w ago
Also, you can just copy the json and generate the class from that VS and Rider have it built-in, and I linked online tools to do that above
hoggy077
hoggy0772w ago
VS has Json gen built-in?! my god, the things you miss
Angius
Angius2w ago
ye Edit -> paste as Json IIRC Or edit -> paste special -> json as classes Something like that
Nick Eh Likes 15
Nick Eh Likes 15OP2w ago
and also just to check there is only one bool so i can do bool name? not bool[] name
Angius
Angius2w ago
Yes
Nick Eh Likes 15
Nick Eh Likes 15OP2w ago
because [] is a array meaning there is more than onme great no way let me check these tools
hoggy077
hoggy0772w ago
this one is indeed correct
Nick Eh Likes 15
Nick Eh Likes 15OP2w ago
wow
hoggy077
hoggy0772w ago
crazy
Angius
Angius2w ago
No description
Nick Eh Likes 15
Nick Eh Likes 15OP2w ago
just done it amazing one thing, you see how some have public, priviate and just class what does these mean? one thing that i dont see in other langaues so i have no clue
Angius
Angius2w ago
Access Modifiers - C#
All types and type members in C# have an accessibility level that controls whether they can be used from other code. Review this list of access modifiers.
Nick Eh Likes 15
Nick Eh Likes 15OP2w ago
makes more sense so if i use public for every class really it wont matter just makes it less secure
hoggy077
hoggy0772w ago
yes
Nick Eh Likes 15
Nick Eh Likes 15OP2w ago
its telling me im wrong
Nick Eh Likes 15
Nick Eh Likes 15OP2w ago
No description
Nick Eh Likes 15
Nick Eh Likes 15OP2w ago
Argument 1: cannot convert from 'object' to 'Newtonsoft.Json.JsonReader'CS1503 class System.String Represents text as a sequence of UTF-16 code units. No quick fixes available
Nick Eh Likes 15
Nick Eh Likes 15OP2w ago
No description
Angius
Angius2w ago
No description
Angius
Angius2w ago
No description
Nick Eh Likes 15
Nick Eh Likes 15OP2w ago
so i add reponseContent instent of playerinfo?
Angius
Angius2w ago
Yes
Nick Eh Likes 15
Nick Eh Likes 15OP2w ago
The name 'responseContent' does not exist in the current contextCS0103
Nick Eh Likes 15
Nick Eh Likes 15OP2w ago
No description
Angius
Angius2w ago
See this deserialization here? Instead of deserializing to list here Deserialize to Root Then use that
Nick Eh Likes 15
Nick Eh Likes 15OP2w ago
so i have to put the json to a var first
Angius
Angius2w ago
Why?
Nick Eh Likes 15
Nick Eh Likes 15OP2w ago
thats what you done i thouight
Angius
Angius2w ago
To have a json string to deserialize You already have a json string to deserialize
Nick Eh Likes 15
Nick Eh Likes 15OP2w ago
so its just this isnt it?
No description

Did you find this page helpful?