C
C#•3y ago
EliasGPS

filter System.string [Answered]

is is possible so select for example only id?
103 Replies
EliasGPS
EliasGPSOP•3y ago
this is a json format but i can't filter on it
MeHow
MeHow•3y ago
u can deserialize it and then select it using linq Newtonsoft.Json package
EliasGPS
EliasGPSOP•3y ago
EliasGPS
EliasGPSOP•3y ago
so something like that?
MeHow
MeHow•3y ago
yes, but you need a class which will represent the data stored in the Json
EliasGPS
EliasGPSOP•3y ago
EliasGPS
EliasGPSOP•3y ago
yaeh so this?
MeHow
MeHow•3y ago
yes exactly
EliasGPS
EliasGPSOP•3y ago
ok thx i will try
MeHow
MeHow•3y ago
then you can do var ids = account.Select(x => x.Email).ToList(); and this will be a list of emails in this case
kocha
kocha•3y ago
you can also use system.text.json if you dont want any extra dependencies 🙂
EliasGPS
EliasGPSOP•3y ago
ohh
MeHow
MeHow•3y ago
But your string will not deserialize to the same type of object as presented on these screenshots
EliasGPS
EliasGPSOP•3y ago
what do you mean?
MeHow
MeHow•3y ago
Well, you have assigned list of users to the "data"
EliasGPS
EliasGPSOP•3y ago
yes
MeHow
MeHow•3y ago
do you need it?
EliasGPS
EliasGPSOP•3y ago
no
MeHow
MeHow•3y ago
do something like this instead
{
"name":"abcd",
"model":3670438162,
"power":21,
},
{
"name":"abc",
"model":3101863448,
"power":22,
}
]
{
"name":"abcd",
"model":3670438162,
"power":21,
},
{
"name":"abc",
"model":3101863448,
"power":22,
}
]
I mean the structure of you file
EliasGPS
EliasGPSOP•3y ago
oh but the probem is i can't do that away
MeHow
MeHow•3y ago
do you store anything else than list of users in your json?
EliasGPS
EliasGPSOP•3y ago
EliasGPS
EliasGPSOP•3y ago
nope it are always the same values
EliasGPS
EliasGPSOP•3y ago
EliasGPS
EliasGPSOP•3y ago
but at the end of the file i also ahve this but i don't need meta i only wnat my data
MeHow
MeHow•3y ago
well I don't understand how you store this json
EliasGPS
EliasGPSOP•3y ago
yeah its a twitter api pull it is a list of my twitter followers
MeHow
MeHow•3y ago
oh okay
EliasGPS
EliasGPSOP•3y ago
so what i do is
MeHow
MeHow•3y ago
can you show me whole structure of this Json? like the whole json on one screeshot
EliasGPS
EliasGPSOP•3y ago
MeHow
MeHow•3y ago
i mean the result of this twitter api pull
EliasGPS
EliasGPSOP•3y ago
yeah but look i have a powerhshell script that does the work for me and i run that script in my c# and get that information in a variable output so do you want to see my powerhsell script or? because i do a convertot)json convertt-json
MeHow
MeHow•3y ago
okay, so if you want to deserialize this json you need a structure that will cover the "data" and "meta" anyway so it will be a class which will contain 2 props: List of Users which is data, and meta
EliasGPS
EliasGPSOP•3y ago
this is how my powerhsell looks like
MeHow
MeHow•3y ago
good if you get the json as the result you need to deserialize it in order to do anything with it in c#
EliasGPS
EliasGPSOP•3y ago
okey uhh
MeHow
MeHow•3y ago
it's very simple but you have to represent the json structure in c# and then it will be easy
EliasGPS
EliasGPSOP•3y ago
ok i will try thx
MeHow
MeHow•3y ago
i dont know much about security but I would delete this screenshot if i was you
EliasGPS
EliasGPSOP•3y ago
i removed my token no problem
MeHow
MeHow•3y ago
cool
EliasGPS
EliasGPSOP•3y ago
but yeah i will remove it the thing is you said i need to deserialize that object but is it an object?
MeHow
MeHow•3y ago
It is a string for now. You have to represent it with a C# object just as it is here
EliasGPS
EliasGPSOP•3y ago
i did it a little different
EliasGPS
EliasGPSOP•3y ago
EliasGPS
EliasGPSOP•3y ago
this is now my output and i'm going to try to make three arrays an array for id name and username
MeHow
MeHow•3y ago
well that's not efficient way of doing it but do as you want I would create a User class
public class User
{
public long Id {get; set;}
public string Name {get; set;}
public string Username {get; set;}
}
public class User
{
public long Id {get; set;}
public string Name {get; set;}
public string Username {get; set;}
}
and iterate through this output and covert it to List<User>
EliasGPS
EliasGPSOP•3y ago
EliasGPS
EliasGPSOP•3y ago
is this more effient without data and meta
MeHow
MeHow•3y ago
yes it's a json
EliasGPS
EliasGPSOP•3y ago
so its easy now
MeHow
MeHow•3y ago
now create a User class as I mentioned there
EliasGPS
EliasGPSOP•3y ago
ow ok thx btw
MeHow
MeHow•3y ago
no problem
EliasGPS
EliasGPSOP•3y ago
but the problem is when it print the type it still says System.strintg string and than i try this method
EliasGPS
EliasGPSOP•3y ago
EliasGPS
EliasGPSOP•3y ago
but it gives me this
EliasGPS
EliasGPSOP•3y ago
EliasGPS
EliasGPSOP•3y ago
EliasGPS
EliasGPSOP•3y ago
this is my code in my main program btw
MeHow
MeHow•3y ago
You must deserialize it to List<TwitterUser>
EliasGPS
EliasGPSOP•3y ago
twitterfollowers is my json format i do that right?
MeHow
MeHow•3y ago
JsonConvert.DeserializeObject<List<TwitterUser>>(twitterFollowers)
EliasGPS
EliasGPSOP•3y ago
EliasGPS
EliasGPSOP•3y ago
thats my class
MeHow
MeHow•3y ago
Yes Check what i sent
EliasGPS
EliasGPSOP•3y ago
MeHow
MeHow•3y ago
var twitterUsers = JsonConvert.DeserializeObject<List<TwitterUser>>(twitterFollowers);
cap5lut
cap5lut•3y ago
thats what i meant by u should learn the basics first.... List<TwitterUser> and TwitterUser are two completely different types, and thats why its complaining that u can not store on in another
MeHow
MeHow•3y ago
use var when declaring a value its much easier but first understand types
EliasGPS
EliasGPSOP•3y ago
yeah but you said i need to use a class
MeHow
MeHow•3y ago
yes class represents SINGLE twitter user and your json contains MULTIPLE so it needs to be an enumerable so you can deserialize multiple users from a json
EliasGPS
EliasGPSOP•3y ago
but now i can't say .id or .name
MeHow
MeHow•3y ago
that's why it needs to be a List, Array, Enumerable etc. yes you can now you have list of them so you have to specify which one of them you want the id of
cap5lut
cap5lut•3y ago
u need to iterate over the elements of the list, each element is of type TwitterAccount and there u can access its properties
MeHow
MeHow•3y ago
if every Id then do this:
var userIds = twitterUsers.Select(x => x.Id).ToList();
var userIds = twitterUsers.Select(x => x.Id).ToList();
EliasGPS
EliasGPSOP•3y ago
ow yeah i see
MeHow
MeHow•3y ago
if you want the first one you can do
var firstUser = twitterUsers[0];
var firstUser = twitterUsers[0];
but remember that if this list is empty you will get an exception so you better be using Linq
EliasGPS
EliasGPSOP•3y ago
i thought that the whole process would be so much easier hahah
MeHow
MeHow•3y ago
haha, no worries, it is easy but you need to understand how c# works
EliasGPS
EliasGPSOP•3y ago
jep my c# skills are very bad
MeHow
MeHow•3y ago
if you get used to it, such operation is a matter of minutes you can always improve, dont worry
EliasGPS
EliasGPSOP•3y ago
thx
MeHow
MeHow•3y ago
youre welcome
EliasGPS
EliasGPSOP•3y ago
oh yeah i see so you mean soùmething like this
EliasGPS
EliasGPSOP•3y ago
EliasGPS
EliasGPSOP•3y ago
yeah yeah thx
MeHow
MeHow•3y ago
Yes
EliasGPS
EliasGPSOP•3y ago
EliasGPS
EliasGPSOP•3y ago
now i made a twitter_id array first a list and than i convert that list to an array
MeHow
MeHow•3y ago
I recommend using lists
EliasGPS
EliasGPSOP•3y ago
because there are alot of benefits they say but the problem is later
MeHow
MeHow•3y ago
what do you want to achieve?
EliasGPS
EliasGPSOP•3y ago
i need to pass an int or string or array to contentful wait a sec
EliasGPS
EliasGPSOP•3y ago
EliasGPS
EliasGPSOP•3y ago
this is my case and i was thinking if i can take twitter information as a theme that would be awesome so first i need to deliver content to contentful and that is my id name and username
MeHow
MeHow•3y ago
i know nothing about this api unfortunately
EliasGPS
EliasGPSOP•3y ago
no noprobel no problem but that is my goal so yeah
MeHow
MeHow•3y ago
You would have to describe what data you need in c# and I can help you with that so it depends on what type of object contentful takes as an argument That's what you should be looking at first
EliasGPS
EliasGPSOP•3y ago
EliasGPS
EliasGPSOP•3y ago
i also thought array
Accord
Accord•3y ago
✅ This post has been marked as answered!

Did you find this page helpful?