-1

I have a json response which is returning me a list of image's URLs and all the URLs are separated by comma(","). I split the response by using split function to separate all the URLs from each other and then I add all the URLs in my list.

But whenever I want to access that list (in the same class) after adding URLs into it, I get nothing. I checked the count of the list and it's 0. Here is the code:

//initialization of list
List<string> productGallery = new List<string>();

//Parsing the json response
var jArray = JArray.Parse(data);
var gallery = jArray[0]["gallery"].Value<string>();
var splitted = gallery.Split(",");

for (int i = 0; i < splitted.Length; i++)
{
    //adding into list
    productGallery.Add(splitted[i]);
}   

Here is the Json response before I am applying Parse method on it

Json response:[
  {
    "id": "2",
    "gallery": "https://sakany.ma//images/sakany-logo.png,https://sakany.ma/images/sub_services/leak-sink.jpg,https://sakany.ma/images/sub_services/shower_head.jpg"
  }
]
    
M Salman
  • 170
  • 1
  • 11
  • Can you please [edit] your question and share a sample of the JSON you are receiving (as text) -- i.e. a [mcve]? – dbc Nov 03 '19 at 15:25
  • @dbc added the json response. Please check – M Salman Nov 03 '19 at 15:31
  • Seems like your code works fine. productGallery.Count = 3. Demo: https://dotnetfiddle.net/3o5vC5 – dbc Nov 03 '19 at 15:33
  • Did you set breakpoints and inspect the vars? – Trevor Nov 03 '19 at 15:35
  • @Çöđěxěŕ I am gettinng the data in my for loop. But list is empty when I access it. – M Salman Nov 03 '19 at 15:38
  • @dbc any idea why its not working for me? Maybe because I have initialized my list as a data member? – M Salman Nov 03 '19 at 15:39
  • You don't need a loop: `List productGallery = gallery.Split(",").ToList();` – Olivier Jacot-Descombes Nov 03 '19 at 15:41
  • @OlivierJacot-Descombes But I want to use this list in other functions too in the same class. – M Salman Nov 03 '19 at 15:43
  • 2
    *any idea why its not working for me?* - if you can share a [mcve], then we can help you. But if the code you are sharing works then how can we help? Try to add just enough code to your question to actually reproduce the problem; see [ask] and https://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/ for tips. – dbc Nov 03 '19 at 15:45
  • You can separate the declaration from the initialization and you can replace the existing list at any time. `List productGallery = new List();` ... `productGallery = gallery.Split(",").ToList();`. Still don't need a loop. Or you can use `productGallery.AddRange(gallery.Split(","));` – Olivier Jacot-Descombes Nov 03 '19 at 15:52
  • @OlivierJacot-Descombes problem is not converting it into list. Problem is when I access that list, it gives me nothing. Do you think ToList() method will resolve the issue? – M Salman Nov 03 '19 at 15:55
  • @OlivierJacot-Descombes I have tried ToList() method and I checked the size of the list after it and it returns 3 but when I call that list from some other function, it returns me 0 again. – M Salman Nov 03 '19 at 16:07
  • Then probably you create a new object from this class containing the list after having filled it. And then you see another empty list. You must access it through the same reference or make the list static. – Olivier Jacot-Descombes Nov 03 '19 at 16:23
  • *and it returns 3 but when I call that list from some other function, it returns me 0 again.* - could you have a local variable inside your method with the same name as a member? If so you would be making the same mistake as [Local variable with same name as instance variable = unexpected results](https://stackoverflow.com/q/10127113/3744182). – dbc Nov 03 '19 at 16:24
  • how do you access that list ?you could make the list as a global variable – Leo Zhu Nov 04 '19 at 02:09

1 Answers1

-1

Seems that splitted is empty. So, something's wrong with your JSON response. Check the jArray[0]["gallery"]. Also, maybe .Value<string>() doesn't work properly.

somerandomdev49
  • 177
  • 1
  • 3
  • 11
  • I have added the json response that I am getting. Can you please review it and tell me what's wrong? Because in my loop, I am getting what I want. – M Salman Nov 03 '19 at 15:33