How to get items from array element json c# - c#

How do i get items from array element json c# ?
I want to get multiple artist names from my call, and put them in a listbox.
I can get the first artist like this etc.:
string url = #"http://api.musixmatch.com/ws/1.1/artist.search?apikey=key&q_artist=LMFAO&format=json";
string content = new WebClient().DownloadString(url);
dynamic artistData = JObject.Parse(content);
string artistName = artistData.message.body.artist_list[0].artist.artist_name;
But how can i get more of the names and put them in a listbox?
Here you can see how the json result looks like
Thanks in advance

You could try something like this
var artists = artistData.message.body.artist_list.Select(x => x.artist);
foreach (var artist in artists)
{
var artistName = artist.artist_name;
listBox.Items.Add(new ListBoxItem(artistName , artistName));
}

Hi you can use the below code snippet to get list of names
var artistNames = artistData.message.body.artist_list.Select(var=>var.artist.artist_name).ToList();
Then you could bind this list to your listbox either by using listbox.items.add function calls or by making use of ItemsSource property

Thanks for trying to help me, I really appreciate it. I figured out i could solve my problem like this:
var artistLength = artistData.message.body.artist_list.Count;
for (int i = 0; i < artistLength; i++)
{
var artistName = artistData.message.body.artist_list[i].artist.artist_name;
//printed in the console for testing
Console.WriteLine(artistName);
listBox1.Items.Add(artistName);
}

Related

C# Selenium - dropdown menu/ combobox

I have a problem selecting from a custom dropdown menu.. I have tried both by using the XPath, CssSelector and Id.
I have added a link to the code here:
Picture of the code
I think i have to access the div class="SelectBox" in order to access the id='ctl00_ctl00_ctl00_MP_Blank_Body_MP_Base_Body_MP_TopSideMenu_Body_ctl00_cboBehandlingstype'
but i keep getting errors.
This is what I'm currently trying but without any luck:
IWebElement test = driver.FindElement(By.XPath("//div[#class='input']//div[#id='ctl00_ctl00_ctl00_MP_Blank_Body_MP_Base_Body_MP_TopSideMenu_Body_ctl00_cboBehandlingstype']"));
Can someone give me a clue on how to get access to the items in the dropdown?
Thank you! :)
You need to use "SelectElement" instead of "IWebElement".
SelectElement mySelect = new SelectElement(yourDriver.FindElement(By.Id("ctl00_ctl00_ctl00_MP_Blank_Body_MP_Base_Body_MP_TopSideMenu_Body_ctl00_cboBehandlingstype")));
mySelect.SelectByText("510111 Normalbehandling");
Try This
var select = driver.FindElementById("ctl00_ctl00_ctl00_MP_Blank_Body_MP_Base_Body_MP_TopSideMenu_Body_ctl00_cboBehandlingstype");
var stringValues = select.Text.Split(new string[] { "\r\n" }, StringSplitOptions.None);
((IJavaScriptExecutor)driver).ExecuteScript("var select = arguments[0]; for(var i = 0; i < select.options.length; i++){ if(select.options[i].text == arguments[1]){ select.options[i].selected = true; } }", select, stringValues[0]);

c# listbox multiselect, selectedItems winforms

I saw many examples of getting the values from selectedItems, but in my case I would like to somehow separate these values. What I mean is, for example if I have in my list options like work home forrest car, I would like to be able after choosing work and home to get both text separated and save them in some string variable.
Now I am doing it this way:
string text = "";
foreach (var item in customListBox1.SelectedItems)
{
text += item.ToString() + " ";
}
Later I am filtering datagridview based on this selecteditems in such way:
var result = list3.Where(Srodek => Srodek.Srodek.category1 == text);
That is why I need them separated. How can I do it?
If something is not clear, please let me know, I will try to explain it more.
You can do something better like this:
string text = string.Join(",", customListBox1.SelectedItems.OfType<Object>().Select(x => x.ToString()).ToArray());
var list = customListBox1.SelectedItems.Cast<string>().ToList();
var result = list3.Where(Srodek => list.Any(x=>x == Srodek.Srodek.category1));

Create a list of items within 'var response'

I have the following code:
var request = new GeocodingRequest();
request.Address = postcode;
request.Sensor = "false";
var response = GeocodingService.GetResponse(request);
var result = response.Results. ...?
I'd very much like to get result as a list, but I can't seem to convert it. I know I can do something like response.Results.ToList<string>();, but have had no luck.
Can anyone help please :)
Well you can just use:
GeocodingResult[] results = response.Results;
or
List<GeocodingResult> results = response.Results.ToList();
If you want a list of strings, you'll need to decide how you want to convert each result into a string. For example, you might use:
List<string> results = response.Results
.Select(result => result.FormattedAddress)
.ToList();
It is defined as:
[JsonProperty("results")]
public GeocodingResult[] Results { get; set; }
if you want to make it list call: response.Results.ToList().
But why do you want to make it list? You can insert items into list, but I don't think you need it.
assuming response.Results is IEnumerable, just make sure System.Linq is available as a namespace and say response.Results.ToList()

Retrieve Value of DropDownList by Index ID

I am trying to retrieve the value of a dropdown list by specifying its indexid but I cant seem to find a way to accomplish this. I dont want it to be the selected value either, basically I need to go through the list, and then add it to an array. I can find all kinds of ways to get it based on the value but not by the index id anyone know how to do this? Im using c#.
string valueAtIndex = myComboBox.Items[SomeIndexValue].Value;
Have you tried lstWhatever.SelectedIndex?
Since the object supports IEnumerable, you can use foreach to loop through, if that is your intention.
Is this what you are looking for?
List<String> theList = new List<string>();
foreach (var item in comboBox1.Items)
{
theList.Add(item.ToString());
}
Where comboBox1 is your dropdown list and i assumed you are talking about a windows forms project.
Or, if you want to us Index Id:
List<string> theList = new List<string>();
for (int i = 0; i < comboBox1.Items.Count; i++)
{
theList.Add(comboBox1.Items[i].ToString());
}
String valueAtIndex = myComboBox.Items[myComboBox.SelectedIndex].ToString();

c# List<string> assignment problem

I'm fairly new to C# so this might be difficult for me to put into words.
I am creating a media application and I have a class (called MediaFile) that contains information about media files (name, size, play count and tags). These are of string, double, int and List<string> types, respectively.
I have a list of these objects, so for example to call MediaFile[2].Tags will refer to a list of "tag" strings (related keywords).
The problem I have is that when I ask the user to enter tags for the selected MediaFile, whenever I try to save those tags to that specific object, the tags get saved to every single object. The code for the assignment currently looks something like this:
MediaFile[lstLibrary.SelectedIndices[0]].Tags.Add(tempTag);
'tempTag' is a string that I'm trying to add into the list of strings, but like I said - even though only one file is selected, the 'tempTag' string gets added into the list of strings of each MediaFile.
Can anyone shed some light on what I'm doing wrong?
Many thanks.
EDIT: Thanks for all of your responses. Whenever I create a MediaFile instance I pass new List<string> to the constructor. Then later on when I go to change this list of strings I discover all of the MediaFiles seem to have the same string reference. Here is the MediaFile class:
public static List<MediaType> MediaFile = new List<MediaType>();
public class MediaType
{
public string Name;
public string Path;
public double Size;
public int Plays;
public List<string> Tags;
// Constructor for adding new files
public MediaType(string path, List<string> tags)
{
Path = path;
Tags = tags;
}
And after I ask the user to select a file to add to the media library:
MediaFile.Add(new MediaType(Path.GetFullPath(file), new List<string>()));
So there are no 'tags' at first, but then later on (and herein lies my problem):
if (tempTag != "")
MediaFile[lstLibrary.SelectedIndices[0]].Tags.Add(tempTag);
}
Any idea? Apologies this post is so long!
That suggests you've added the same string reference to all the MediaFiles. In other words, you might have done:
List<string> tags = new List<string>();
for (int i = 0; i < 100; i++)
{
MediaFile file = new MediaFile(tags);
MediaFiles.Add(file);
}
This is very easy to test - just check whether MediaFile[0].Tags == MediaFile[1].Tags - I suspect you'll find that evaluates to True, meaning you're using the same list for both files.
Instead, you should create a new List<string> for each MediaFile instance.
If you could post some code, that would help us to pin down the problem more precisely, of course...
It looks like you have set all of the MediaFiles[i].Tags to be references to the same instance of a list.
Where do you put stuff in MediaFile? How do you do it?
My guess is that you're inserting the same MediaFile object several time in the list. So, when you modify one, you end up modifying all of them because, after all, they are the same object.
It sounds like you've set all the objects in your list to use the same instance of the Tags list
I suspect that you're initialising your objects incorrectly. You're probably (effectively) creating them like this:
List<string> tags = new List<string>();
MediaFile m1 = new MediaFile();
m1.Tags = tags;
MediaFile m2 = new MediaFile();
m2.Tags = tags;
That is, the Tags property of each MediaFile refers to the same actual List<string> object. Thus, when you add a string to m1.Tags, you're also adding it to m2.Tags.
What you want to do instead is:
MediaFile m1 = new MediaFile();
m1.Tags = new List<string>();
MediaFile m2 = new MediaFile();
m2.Tags = new List<string>();
Does your Tags property have a set accessor?
Or do you have code that looks (something) like this?
var tags = new List<string>();
for (int i = 0; i < MediaFile.Count; ++i)
{
MediaFile[0] = new MediaFile(tags);
}
If so then each of your MediaFile instances is sharing the same internal List<string>.
You should not have a set accessor on this property at all, actually, nor should the MediaFile constructor be willing to take some external List<string> (unless it's going to copy it). Instead assign it only in the constructor for MediaFile to a new List<string>(); this way you will ensure that each instance gets its own list:
public MediaFile()
{
this.Tags = new List<string>();
}
Or:
public MediaFile(IEnumerable<string> tags)
{
this.Tags = new List<string>(tags);
}

Categories