✅ Can't figure out json logic
the purpose of this function is to simply return the number of users in the json file. The json file is structured like this
and I want to return the number of
"something_here1"
's. Thanks in advance43 Replies
That's not a list
{}
in json denotes an objectoh ok. so then do I need to iterate through the object, add the UserModel's to a list and then return the list count?
You can't "iterate through object"
Could also be a dictionary, I guess
could be a
Dictionary<string, T>
With T
being, well, the typewhat about this?
that doesn't change the fact the json isn't a list
You cannot deserialize a not-list to a list
it's a
or a dictionary like zzz said
ok I think I got myself messed up a little bit
so when the user is written to the json file, it's like this
that's done using a UserModel
That would be a
Dictionary<string, string>
A dictionary of username-password pairsNo
I said the wrong thing
myfault
it's written like this
This would deserialize to
List<UserModel>
So... a dictionary after allyes. I got mixed up
or, clearer denoted as
i didn't know I could do that
I still don't understand why ur so focused on the json and not on your models
if you control the json, then just worry about what your models look like
ok. I wasn't consice enough with my issue. I'm trying to check the json file to see if any usernames exists, and if so then it's going to show screen A otherwise its going to show screen B
which I believe I can do with a simple null check, right?
Deserialize -> try to find -> see if found
that's fine but u dont need to worry about what the json looks like
if u control the models u know what you expect from your jsons to be
ok. I'll try and remember that
I guess the issue stems from the fact, that the users are serialized as username-password pairs
Instead of user objects
Which is confusing, since your first instinct when deserializing would be to try to deserialize it to a list of users
if you're looking from the json perspective maybe
@Mekasu0124 what I suggest to u is, create a console project and serialize different models and see what it looks like
and then deserialize using the same models as a practice
it will clear a lot of things
you will notice a very clear pattern
like when its {} and when its [] etc
normally when you have no control over the json, its when u need to worry about what the json looks like to recreate the models and some APIs make it really nasty some times to the point where u need to use discriminators, unions, etc.
but when you control the models you can model the json anyway u like so u dont have to worry about what it looks like as the final string so long as u make your models make sense to your needs.
I wasn't concerned with the layout of the json file. I just showed what the file looked like as a reference with my issue
I just want to be able to open the json file and see if it looks like
{}
or if it actually contains data and return a T/F valueyes I understand that, and that showed us you dont understand the different json layouts for when u serialize something
ok
actually, I have another way I'm going to go about it. Thanks for the help guys
leowest
REPL Result: Success
Result: string
Quoted by
<@1102729783969861782> from #bot-spam (click here)
Compile: 265.768ms | Execution: 25.067ms | React with ❌ to remove this embed.
that is just an empty list serialized []
leowest
REPL Result: Success
Result: string
Quoted by
<@1102729783969861782> from #bot-spam (click here)
Compile: 363.129ms | Execution: 34.200ms | React with ❌ to remove this embed.
which was the example u provided above
that is why I suggest the console app to try models at it will help u visualize better the json and the models
leowest
REPL Result: Success
Result: string
Quoted by
<@1102729783969861782> from #bot-spam (click here)
Compile: 390.620ms | Execution: 51.849ms | React with ❌ to remove this embed.
@Mekasu0124 anyway I just wanted to illustrate some of the differences, hope it helps u visualize things.
here i will show you how to do it. You can use DataContract and DataMember
This is a wrapper I made for Serializing and Deserializing objects to from json
if the outside of your json uses square brackets. That's a list or array right. So for example Suppose we have this class:
I appreciate it but I’m actually going to use a database instead. It’s easier to work with
namespace Chat.Messages.Client.Messages
{
[DataContract]
public class Example
{
[DataMember(Name = "username")]
public string Username { get; protected set; }
[DataMember(Name = "password")]
public string Password { get; protected set; }
}
}
suppose you have this json [{"username":"dfdffd", "password":"fdfddf"}, {"username":"anotheruser", "password":"dfdfdf"}]
you could parse it like this
new NativeJsonParser().Deserialize<Example[]> (theJsonString);
🙂
its really not. You're going to need json to speak to servers through websockets and for configuration files and stuff anyway 🙂
what do you mean?
once you get the hang of parsing json using DataContract it's really easy. Just play with it till you get the hang of it :). When you write a class and decorate properties with DataMember you have to remember to provide a set method too. It requires that even if you're serializing it. You can make the set method protected.
you can also nest objects with it.
so a property in one class can be another class.
lol I do good enough to create the avalonia project, and get basic functionality going on my applications 😂 I'm just toying around with the framework at this time. I'm not seriously producing anything, but I will keep this in mind and reference back to it when the time comes ❤️
Never used avalonia but it seems to be popular :). Ok