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
105 Replies
oof, newtonsoft
this has kind of done my head in now, im so confused on why it doesnt work
Are you serializing to an object? & maybe consider using System.Text.Json instead
im not actually sure, and ill have a look at the system.text.json
but i dont think i am
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
its pretty long, over 1k lines long and not even including the other files
thats only the program file
$code
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/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"]
BlazeBin - fagepsfzngtv
A tool for sharing your source code with the world!
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
Deserialize to a good and proper object
Forget
dynamic
ever existed^
Erase it from your mind
It's not a thing
It's fake
-# 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
The universe is worse for it's existence
isnt that what im trying to do?
if ((bool)playerInfo["isJunior"] == true) {
checkJunior = true;
} else {
checkJunior = false;
}
No
You deserialize to
dynamic
where should i deserializ it to
And to
List<Dictionary<string, object>>
To a good and proper object
$jsongen might helpInstantly 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.
Make a proper class, deserialize to that
would that be using
JsonConvert.DeserializeObject<PlayerInfo>(json);
okay let me try
Ideally without Newtonsoft, but yeah
JsonSerializer.Deserialize<PlayerInfo>(json)
if we're writing code like it's 2025is your
isJunior
a string by any chance?"isJunior":false
thats the json that comes back
the isJunior it self is
the value it holds isnt
ok, nvm then. That was the only reason I could think of casting failing to brazenly
wait but then how would i use that
Wym?
my json is from playerInfo["isJunior"]
but what is <PlayerInfo>
A class that describes the JSON
that takes the Json & returns you the generic
so that can be anything?
A class that describes the JSON
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
you feed it the original json string
Angius
REPL Result: Success
Console Output
Compile: 569.633ms | Execution: 62.087ms | React with ❌ to remove this embed.
but thats how i get it?
oh right

responseContent
is the JSON you get from that requestMan, this is not the newtonsoft I remember
And higher up, this is also where you parse some JSON

To a
dynamic
this time
So burn it to the ground as well and use a proper classdefinitely use a proper type
okay ill try my best
but only been coding in c sharp for not very long
thats ok
everyone starts somewhere
yeah exactly its a big change from something simple like python
hell, some of us have been doing C# for decades and are still learning
Now, most people probably don't start with a bunch of
[DllImport]
s and 700+ lines of code, but...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
forking a project a few days in, very bold
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; }
}
No dictionaries
Json string -> deserializer -> class instance
what is this called then?
{
public string Foo { get; set; }
public int Count { get; set; }
public bool[] Bools { get; set; }
}
No intermediary dictionaries, no nothing
A class
Well, you stripped away the
class Unga
So not a class in this case
But, yeah, a classso 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?

This is a whole thing
A class and its properties
oh i get it now
You can't just pick and choose bits and pieces from it
thats just an example
Yeah
so i need its own class?
yes
Yes
One that describes your Json
but i can call from outside correct?
depends what you mean by outside
like just not in that class
outside the function? yes
outside the class

Notice
And, ideally, just place it in a different file
yes got it
Your code is already humongous as it is, you don't want to add another 50 lines for a class to deserialize to
thats true
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
VS has Json gen built-in?!
my god, the things you miss
ye
Edit -> paste as Json IIRC
Or edit -> paste special -> json as classes
Something like that
and also just to check there is only one bool so i can do bool name?
not bool[] name
Yes
because [] is a array meaning there is more than onme
great
no way
let me check these tools
this one is indeed correct
wow
crazy

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
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.
makes more sense
so if i use public for every class
really it wont matter
just makes it less secure
yes
its telling me im wrong

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



so i add reponseContent instent of playerinfo?
Yes
The name 'responseContent' does not exist in the current contextCS0103

See this deserialization here?
Instead of deserializing to list here
Deserialize to
Root
Then use thatso i have to put the json to a var first
Why?
thats what you done i thouight
To have a json string to deserialize
You already have a json string to deserialize
so its just this isnt it?
