-3

I have following big string and I want all .jpg file name with extension in to string List in C# but I don't know how to get it.

[{\"url\":\"https:\\/\\/\\/data\\/menus\\/799\\/799\\/dd2d49c78b1fe3a9cea5761d90132ff1.jpg\",\"href\":\"https:\\/\\/\\/data\\/menus\\/799\\/799\\/dd2d49c78b1fe3a9cea5761d90132ff1.jpg\",\"filename\":\"dd2d49c78b1fe3a9cea5761d90132ff1.jpg\",\"url_master\":\"menus_original\\/799\\/799\\/dd2d49c78b1fe3a9cea5761d90132ff1.jpg\",\"path_master\":\"\\/home\\/foodie\\/zomato_data\\/menus_original\\/799\\/799\\/dd2d49c78b1fe3a9cea5761d90132ff1.jpg\",\"data_center\":\"\",\"menu_type\":\"FOOD\",\"title\":\"FOOD\",\"menu_type_class\":\"FOOD\",\"real_menu_type\":\"FOOD\",\"is_salt_special_menu\":0,\"start_date\":\"\",\"consumer_upload\":0,\"start_date_formatted\":\"\",\"end_date\":\"\",\"end_date_formatted\":\"\",\"id\":131698200},{\"url\":\"https:\\/\\/\\/data\\/menus\\/799\\/799\\/f9f923c43b6b2d2a87ad8ce22b9995da.jpg\",\"href\":\"https:\\/\\/\\/data\\/menus\\/799\\/799\\/f9f923c43b6b2d2a87ad8ce22b9995da.jpg\",\"filename\":\"f9f923c43b6b2d2a87ad8ce22b9995da.jpg\",\"url_master\":\"menus_original\\/799\\/799\\/f9f923c43b6b2d2a87ad8ce22b9995da.jpg\",\"path_master\":\"\\/home\\/foodie\\/zomato_data\\/menus_original\\/799\\/799\\/f9f923c43b6b2d2a87ad8ce22b9995da.jpg\",\"data_center\":\"\",\"menu_type\":\"FOOD\",\"title\":\"FOOD\",\"menu_type_class\":\"FOOD\",\"real_menu_type\":\"FOOD\",\"is_salt_special_menu\":0,\"start_date\":\"\",\"consumer_upload\":0,\"start_date_formatted\":\"\",\"end_date\":\"\",\"end_date_formatted\":\"\",\"id\":131698203},{\"url\":\"https:\\/\\/\\/data\\/menus\\/799\\/799\\/ea3117de65882f14723480841940b5b1.jpg\",\"href\":\"https:\\/\\/\\/data\\/menus\\/799\\/799\\/ea3117de65882f14723480841940b5b1.jpg\",\"filename\":\"ea3117de65882f14723480841940b5b1.jpg\",\"url_master\":\"menus_original\\/799\\/799\\/ea3117de65882f14723480841940b5b1.jpg\",\"path_master\":\"\\/home\\/foodie\\/zomato_data\\/menus_original\\/799\\/799\\/ea3117de65882f14723480841940b5b1.jpg\",\"data_center\":\"\",\"menu_type\":\"FOOD\",\"title\":\"FOOD\",\"menu_type_class\":\"FOOD\",\"real_menu_type\":\"FOOD\",\"is_salt_special_menu\":0,\"start_date\":\"\",\"consumer_upload\":0,\"start_date_formatted\":\"\",\"end_date\":\"\",\"end_date_formatted\":\"\",\"id\":131698204}]

I want below list from sting :

dd2d49c78b1fe3a9cea5761d90132ff1.jpg
dd2d49c78b1fe3a9cea5761d90132ff1.jpg
dd2d49c78b1fe3a9cea5761d90132ff1.jpg
f9f923c43b6b2d2a87ad8ce22b9995da.jpg
f9f923c43b6b2d2a87ad8ce22b9995da.jpg

Thank You... in advance

RAJNIK PATEL
  • 1,039
  • 1
  • 17
  • 30

2 Answers2

1

If you just need .jpg file name, you can try this simple Regex:

\w+\.jpg

Demo: https://regex101.com/r/KMWtZY/1

You can use it with C# as follows:

var regex = new Regex(@"\w+\.jpg");
return regex.Matches(strInput);

Source: 1

Community
  • 1
  • 1
Ibrahim
  • 6,006
  • 3
  • 39
  • 50
0

Your string seems like it is a JSON String. and thus it would be easier and convienent approach to desealize the JSON into an object and fetch the values like

Create a class for the Object of your JSON String

public class YourData
{
    public string url { get; set; }
    public string href { get; set; }
    public string filename { get; set; }
    public string url_master { get; set; }
    public string path_master { get; set; }
    public string data_center { get; set; }
    public string menu_type { get; set; }
    public string title { get; set; }
    public string menu_type_class { get; set; }
    public string real_menu_type { get; set; }
    public int is_salt_special_menu { get; set; }
    public string start_date { get; set; }
    public int consumer_upload { get; set; }
    public string start_date_formatted { get; set; }
    public string end_date { get; set; }
    public string end_date_formatted { get; set; }
    public int id { get; set; }
}

Than

//From wherever you are reading it.
string jsonstr = "Your Json String";
//I Removed all the / and \ from the string
jsonstr = jsonstr.Replace("/", "");
jsonstr = jsonstr.Replace("\\", "");
//at last of the string you have something like this 
//;\n        .menuTypes = [\"DEFAULT\",\"FOOD\",\"BAR\",\"DELIVERY\",\"SPECIAL\",\"TAKEAWAY\",\"INTERNAL\"];\n
//which is not the part of the JSON string. So I removed that part as well to make it a valid JSON
jsonstr = jsonstr.Remove(jsonstr.IndexOf(";n"));
//Console.WriteLine(jsonstr); //You can uncomment it to see how JSON looks after cleaning.
//Just Deserialize the JSON
List<YourData> yd = JsonConvert.DeserializeObject<List<YourData>>(jsonstr);
//Loop to get all the filenames or any other fields you want 
foreach(YourData ydd in yd)
{
    Console.WriteLine(ydd.filename);
}
Mohit S
  • 13,723
  • 6
  • 34
  • 69