none
c# create a string list<string,string> and sort to a combo box avoiding duplicates ? RRS feed

  • Question

  • ok i have a json file witch i parsed into width hight 

    and i want to store this in a list and add to combo box avoiding duplicates witch height entry's 

    this is part of the json code sorry its huge so only did little sniplet 

    "/i/models/1dbac27e101440bdbd109a793b7bba82/textures?optimized=1": {
        "results": [
          {
            "updatedAt": "2018-03-12T18:23:49.016712",
            "images": [
              {
                "updatedAt": "2018-03-12T18:23:48.97236",
                "createdAt": "2018-03-12T18:23:48.972321",
                "size": 1779,
                "options": {},
                "uid": "82e6f3dbb1b84317b34ff9f819cb4294",
                "url": "https://media.sketchfab.com/urls/1dbac27e101440bdbd109a793b7bba82/dist/textures/02ca517770ae4ad3b8005fc28be05717/82e6f3dbb1b84317b34ff9f819cb4294.png",
                "width": 32,
                "height": 32
              },
              {
                "updatedAt": "2018-03-12T18:23:47.687583",
                "createdAt": "2018-03-12T18:23:47.687562",
                "size": 2120744,
                "options": {},
                "uid": "dc70a1d7a3444bc0b2d6aae81a421b0a",
                "url": "https://media.sketchfab.com/urls/1dbac27e101440bdbd109a793b7bba82/dist/textures/02ca517770ae4ad3b8005fc28be05717/dc70a1d7a3444bc0b2d6aae81a421b0a.png",
                "width": 2048,
                "height": 2048
              },
              {
                "updatedAt": "2018-03-21T19:39:07.335926",
                "createdAt": "2018-03-21T19:39:07.335891",
                "size": 114516,
                "options": {
                  "quality": 92,
                  "format": "RGB"
                },
                "uid": "89012757f3c540ff85b583f94ecc53e7",
                "url": "https://media.sketchfab.com/urls/1dbac27e101440bdbd109a793b7bba82/dist/textures/02ca517770ae4ad3b8005fc28be05717/89012757f3c540ff85b583f94ecc53e7.jpeg",
                "width": 1024,
                "height": 1024
              },
              {
                "updatedAt": "2018-03-21T19:39:08.121997",
                "createdAt": "2018-03-21T19:39:08.121976",
                "size": 51684,
                "options": {
                  "quality": 95,
                  "format": "RGB"
                },
                "uid": "5daed444d5e04c96ac8c84ce064fb32b",
                "url": "https://media.sketchfab.com/urls/1dbac27e101440bdbd109a793b7bba82/dist/textures/02ca517770ae4ad3b8005fc28be05717/5daed444d5e04c96ac8c84ce064fb32b.jpeg",
                "width": 512,
                "height": 512
              },
              {
                "updatedAt": "2018-03-21T19:39:08.190984",
                "createdAt": "2018-03-21T19:39:08.190962",
                "size": 17008,
                "options": {
                  "quality": 95,
                  "format": "RGB"
                },
                "uid": "499f5b9dd0e94095a7249713ff597d2e",
                "url": "https://media.sketchfab.com/urls/1dbac27e101440bdbd109a793b7bba82/dist/textures/02ca517770ae4ad3b8005fc28be05717/499f5b9dd0e94095a7249713ff597d2e.jpeg",
                "width": 256,
                "height": 256
              },
              {
                "updatedAt": "2018-03-21T19:39:08.264038",
                "createdAt": "2018-03-21T19:39:08.264017",
                "size": 5871,
                "options": {
                  "quality": 95,
                  "format": "RGB"
                },
                "uid": "bfc4fa4d25014df9b9fae1450d48f93a",
                "url": "https://media.sketchfab.com/urls/1dbac27e101440bdbd109a793b7bba82/dist/textures/02ca517770ae4ad3b8005fc28be05717/bfc4fa4d25014df9b9fae1450d48f93a.jpeg",
                "width": 128,
                "height": 128
              },
              {
                "updatedAt": "2018-03-21T19:39:08.30114",
                "createdAt": "2018-03-21T19:39:08.301119",
                "size": 2057,
                "options": {
                  "quality": 95,
                  "format": "RGB"
                },
                "uid": "8c42dc051a004073b3b0f449cf237eb3",
                "url": "https://media.sketchfab.com/urls/1dbac27e101440bdbd109a793b7bba82/dist/textures/02ca517770ae4ad3b8005fc28be05717/8c42dc051a004073b3b0f449cf237eb3.jpeg",
                "width": 64,
                "height": 64
              },
              {
                "updatedAt": "2018-03-21T19:39:08.33484",
                "createdAt": "2018-03-21T19:39:08.334818",
                "size": 267563,
                "options": {
                  "quality": 88,
                  "format": "RGB"
                },
                "uid": "948f9fb9389c42c7986bee4ce3cd3c2f",
                "url": "https://media.sketchfab.com/urls/1dbac27e101440bdbd109a793b7bba82/dist/textures/02ca517770ae4ad3b8005fc28be05717/948f9fb9389c42c7986bee4ce3cd3c2f.jpeg",
                "width": 2048,
                "height": 2048
              }
            ],
            "createdAt": "2018-03-12T18:23:47.679882",
            "colorSpace": "unknown",
            "uid": "02ca517770ae4ad3b8005fc28be05717",
            "name": "SCOPE_03_Base_Color.png"
          },
          {
            "updatedAt": "2018-03-11T18:03:33.108193",
            "images": [
              {
                "updatedAt": "2018-03-11T18:03:31.038732",
                "createdAt": "2018-03-11T18:03:31.038718",
                "size": 23479,
                "options": {},
                "uid": "bfb40239c507478c80142c8b85147082",
                "url": "https://media.sketchfab.com/urls/1dbac27e101440bdbd109a793b7bba82/dist/textures/031f7aa6fd9641a1a708eb90591464a5/bfb40239c507478c80142c8b85147082.png",
                "width": 256,
                "height": 256
              },
              {
                "updatedAt": "2018-03-11T18:03:33.042656",
                "createdAt": "2018-03-11T18:03:33.04262",
                "size": 480,
                "options": {},
                "uid": "a42964e1fd424c7b9c87455ab8a11cde",
                "url": "https://media.sketchfab.com/urls/1dbac27e101440bdbd109a793b7bba82/dist/textures/031f7aa6fd9641a1a708eb90591464a5/a42964e1fd424c7b9c87455ab8a11cde.png",
                "width": 32,
                "height": 32
              },
              {
                "updatedAt": "2018-03-21T19:38:47.640418",
                "createdAt": "2018-03-21T19:38:47.640381",
                "size": 118,
                "options": {
                  "quality": 100,
                  "format": "A"
                },
                "uid": "0f9a1bf59b804ba6b19ae1dbf4a10342",
                "url": "https://media.sketchfab.com/urls/1dbac27e101440bdbd109a793b7bba82/dist/textures/031f7aa6fd9641a1a708eb90591464a5/0f9a1bf59b804ba6b19ae1dbf4a10342.png",
                "width": 128,
                "height": 128
              },
              {
                "updatedAt": "2018-03-21T19:38:47.679613",
                "createdAt": "2018-03-21T19:38:47.679593",
                "size": 108,
                "options": {
                  "quality": 100,
                  "format": "A"
                },
                "uid": "e827e6a9931544138686c72f6491ea56",
                "url": "https://media.sketchfab.com/urls/1dbac27e101440bdbd109a793b7bba82/dist/textures/031f7aa6fd9641a1a708eb90591464a5/e827e6a9931544138686c72f6491ea56.png",
                "width": 64,
                "height": 64
              },
              {
                "updatedAt": "2018-03-21T19:38:47.71363",
                "createdAt": "2018-03-21T19:38:47.713606",
                "size": 124,
                "options": {
                  "quality": 95,
                  "format": "A"
                },
                "uid": "9b40026f390d4c009cbff32a3f393477",
                "url": "https://media.sketchfab.com/urls/1dbac27e101440bdbd109a793b7bba82/dist/textures/031f7aa6fd9641a1a708eb90591464a5/9b40026f390d4c009cbff32a3f393477.png",
                "width": 256,
                "height": 256
              }
            ],
            "createdAt": "2018-03-11T18:03:31.032536",
            "colorSpace": "unknown",
            "uid": "031f7aa6fd9641a1a708eb90591464a5",
            "name": "DIODE_Opacity.png"
          },
          {
            "updatedAt": "2018-03-21T19:32:38.83781",
            "images": [
              {
                "updatedAt": "2018-03-21T19:32:36.505361",
                "createdAt": "2018-03-21T19:32:36.505347",
                "size": 7765255,
                "options": {},
                "uid": "a5019128261c4b74bf01fa6301bc4816",
                "url": "https://media.sketchfab.com/urls/1dbac27e101440bdbd109a793b7bba82/dist/textures/036651068d404bd2b31b12afdb891c71/a5019128261c4b74bf01fa6301bc4816.png",
                "width": 4096,
                "height": 4096
              },
              {
                "updatedAt": "2018-03-21T19:32:38.767988",
                "createdAt": "2018-03-21T19:32:38.767952",
                "size": 1284,
                "options": {},
                "uid": "a25b8e90036b4ef48b904508a5ed9a3f",
                "url": "https://media.sketchfab.com/urls/1dbac27e101440bdbd109a793b7bba82/dist/textures/036651068d404bd2b31b12afdb891c71/a25b8e90036b4ef48b904508a5ed9a3f.png",
                "width": 32,
                "height": 32
              },
              {
                "updatedAt": "2018-03-21T19:39:58.097489",
                "createdAt": "2018-03-21T19:39:58.097461",
                "size": 279308,
                "options": {
                  "quality": 88,
                  "format": "RGB"
                },
                "uid": "a63668e0364c429d939cb524e55b6168",
                "url": "https://media.sketchfab.com/urls/1dbac27e101440bdbd109a793b7bba82/dist/textures/036651068d404bd2b31b12afdb891c71/a63668e0364c429d939cb524e55b6168.jpeg",
                "width": 2048,
                "height": 2048
              },
              {
                "updatedAt": "2018-03-21T19:39:59.067947",
                "createdAt": "2018-03-21T19:39:59.067929",
                "size": 122382,
                "options": {
                  "quality": 92,
                  "format": "RGB"
                },
                "uid": "beabf154bbb349ab935d805cc2d04dfa",
                "url": "https://media.sketchfab.com/urls/1dbac27e101440bdbd109a793b7bba82/dist/textures/036651068d404bd2b31b12afdb891c71/beabf154bbb349ab935d805cc2d04dfa.jpeg",
                "width": 1024,
                "height": 1024
              },
              {
                "updatedAt": "2018-03-21T19:39:59.209492",
                "createdAt": "2018-03-21T19:39:59.209475",
                "size": 53597,
                "options": {
                  "quality": 95,
                  "format": "RGB"
                },
                "uid": "d9c80ba150554cb085fa009f54436cae",
                "url": "https://media.sketchfab.com/urls/1dbac27e101440bdbd109a793b7bba82/dist/textures/036651068d404bd2b31b12afdb891c71/d9c80ba150554cb085fa009f54436cae.jpeg",
                "width": 512,
                "height": 512
              },
              {
                "updatedAt": "2018-03-21T19:39:59.242606",
                "createdAt": "2018-03-21T19:39:59.242588",
                "size": 17203,
                "options": {
                  "quality": 95,
                  "format": "RGB"
                },
                "uid": "edb61e90373a40a290dd04b39e527e40",
                "url": "https://media.sketchfab.com/urls/1dbac27e101440bdbd109a793b7bba82/dist/textures/036651068d404bd2b31b12afdb891c71/edb61e90373a40a290dd04b39e527e40.jpeg",
                "width": 256,
                "height": 256
              },
              {
                "updatedAt": "2018-03-21T19:39:59.271927",
                "createdAt": "2018-03-21T19:39:59.271911",
                "size": 5576,
                "options": {
                  "quality": 95,
                  "format": "RGB"
                },
                "uid": "ca1103af5dba41ba86cd276b79b4c85a",
                "url": "https://media.sketchfab.com/urls/1dbac27e101440bdbd109a793b7bba82/dist/textures/036651068d404bd2b31b12afdb891c71/ca1103af5dba41ba86cd276b79b4c85a.jpeg",
                "width": 128,
                "height": 128
              },
              {
                "updatedAt": "2018-03-21T19:39:59.303287",
                "createdAt": "2018-03-21T19:39:59.303273",
                "size": 1806,
                "options": {
                  "quality": 95,
                  "format": "RGB"
                },
                "uid": "9943edd2e121422ea74d8dd5f1354ece",
                "url": "https://media.sketchfab.com/urls/1dbac27e101440bdbd109a793b7bba82/dist/textures/036651068d404bd2b31b12afdb891c71/9943edd2e121422ea74d8dd5f1354ece.jpeg",
                "width": 64,
                "height": 64
              },
              {
                "updatedAt": "2018-03-21T19:39:59.334431",
                "createdAt": "2018-03-21T19:39:59.334416",
                "size": 905360,
                "options": {
                  "quality": 88,
                  "format": "RGB"
                },
                "uid": "0e5bc02f83924827ace1ddfbbd56187d",
                "url": "https://media.sketchfab.com/urls/1dbac27e101440bdbd109a793b7bba82/dist/textures/036651068d404bd2b31b12afdb891c71/0e5bc02f83924827ace1ddfbbd56187d.jpeg",
                "width": 4096,
                "height": 4096
              }
            ],
            "createdAt": "2018-03-21T19:32:36.49973",
            "colorSpace": "unknown",
            "uid": "036651068d404bd2b31b12afdb891c71",
            "name": "RECEIVER_Base_Color.png"
          },

    so once its found say 4096 x 4096 it only add the value in once as it does have a lot of textures and there are multiple concordances of the same sizes on different textures

    and this is the code i am using to get the with and height but i not sure how to add it all into a list then put hight and width to a combo box form the list as this list will be static list 

    to have a working program currently i just made it so if its == 2048 x 2048 but i want the user to be able to choose 

    // start to grab textures var jsonContent = File.ReadAllText(elfenliedprogsettings.Read("DOWNLOADPATH") + "\\textures.json"); var data = (JObject)JsonConvert.DeserializeObject(jsonContent); var texturesNodeKey = "/i/models/" + texturemodel + "/textures?optimized=1"; var images = new Dictionary<string, string>(); foreach (var resultNode in data[texturesNodeKey]["results"]) { var name = resultNode["name"].ToObject<string>(); Name = name; foreach (var imageNode in resultNode["images"]) { var height = imageNode["height"].ToObject<long>(); var width = imageNode["width"].ToObject<long>(); var url = imageNode["url"].ToObject<string>(); Url = url; if (height == 2048 && width == 2048) { need to do something here to store all height and width the json has but not sure

    how to accomplish this. } } }


     thanks in advance elfenliedtopfan5
    Friday, October 4, 2019 1:27 PM

All replies

  • Don't use the JSON directly, use a C# type to contain that information and then LINQ can be used to get exactly what you want and it has the objects already contained for you.

    public class Texture 
    {
       public DateTime UpdatedAt { get; set; }
       public List<TextureImage> Images { get; } = new List<TextureImage>();
    }
    
    public class TextureImage
    {
       public DateTime UpdatedAt { get; set; }
       public DateTime CreatedAt { get; set; }
       public int Size { get; set; }  
       //public object Options { get; set; }
       public string Uid { get; set; }
       public string Url { get; set; }
       public int Width { get; set; }
       public int Height { get; set; }
    }

    The JSON you post is not a valid root JSON object so it is clearly nested in something larger. I'm ignoring all that since you didn't post it. Now just read the objects.

    //Trying to use your JObject that you already have but this is a non-standard - guessing the logic here to get the JSON for just the results
    var resultsJson = data[texturesNodeKey]["results"].ToString();
    
    //Convert to real types
    var results = JsonConvert.DeserializeObject<IEnumerable<Texture>>(resultsJson);
    
    //Use LINQ to find what you want - this gets you all textures that have at least one image of the desired size
    var matches = from r in results
                  where r.Images.Any(i => i.Height == 2048 && i.Width == 2048)
                  select r;
    
    //This gives you the images themselves but you lose access to the parent texture
    var matches2 = results.SelectMany(t => t.Images).Where(i => i.Height == 2048 && i.Width == 2048);
    
    
    


    Michael Taylor http://www.michaeltaylorp3.net

    Friday, October 4, 2019 2:00 PM
    Moderator
  • Don't use the JSON directly, use a C# type to contain that information and then LINQ can be used to get exactly what you want and it has the objects already contained for you.

    public class Texture 
    {
       public DateTime UpdatedAt { get; set; }
       public List<TextureImage> Images { get; } = new List<TextureImage>();
    }
    
    public class TextureImage
    {
       public DateTime UpdatedAt { get; set; }
       public DateTime CreatedAt { get; set; }
       public int Size { get; set; }  
       //public object Options { get; set; }
       public string Uid { get; set; }
       public string Url { get; set; }
       public int Width { get; set; }
       public int Height { get; set; }
    }

    The JSON you post is not a valid root JSON object so it is clearly nested in something larger. I'm ignoring all that since you didn't post it. Now just read the objects.

    //Trying to use your JObject that you already have but this is a non-standard - guessing the logic here to get the JSON for just the results
    var resultsJson = data[texturesNodeKey]["results"].ToString();
    
    //Convert to real types
    var results = JsonConvert.DeserializeObject<IEnumerable<Texture>>(resultsJson);
    
    //Use LINQ to find what you want - this gets you all textures that have at least one image of the desired size
    var matches = from r in results
                  where r.Images.Any(i => i.Height == 2048 && i.Width == 2048)
                  select r;
    
    //This gives you the images themselves but you lose access to the parent texture
    var matches2 = results.SelectMany(t => t.Images).Where(i => i.Height == 2048 && i.Width == 2048);
    
    


    Michael Taylor http://www.michaeltaylorp3.net

    i have tyied this before the json file is made in another application so i use this method to as it changes a lot but the way i am parsing this will always be the same and this is the snippet of it the file is like 54,000 lines so trying to do it this way would not work for me as my program checks them based on urls and each one is different only thing that stays the same is how i access the textures 
    Friday, October 4, 2019 2:21 PM
  • Yes that is why you put this very specific logic into a method. The method takes the raw JObject you posted in your original code that comes from that "always different" root object path that you had. This method gets its contents, converts it to typed values and then allows you easy access to the data. If you cannot get this to work then please post your updated code using the logic I posted as a method in your larger code along with the issues you're having.

    Michael Taylor http://www.michaeltaylorp3.net

    Friday, October 4, 2019 2:55 PM
    Moderator
  • Hi elfenliedtopfan2, 

    Thank you for posting here.

    For your question, you want to store width and height in a list and then put height and width to a combox avoiding duplicates.

    I make a test on my side and you can refer to it.

            List<int> lst1 = new List<int>();
            List<int> lst2 = new List<int>();
            private void Button1_Click(object sender, EventArgs e)
            {
    
                string path = @"your path";
                var jsonContent = File.ReadAllText(path);
                var data = (JObject)JsonConvert.DeserializeObject(jsonContent);
     
                var images = new Dictionary<string, string>();
                foreach (var resultNode in data["results"])
                {
                    foreach (var imageNode in resultNode["images"])
                    {
                        var height = imageNode["height"].ToObject<int>();
                        var width = imageNode["width"].ToObject<int>();
                        var url = imageNode["url"].ToObject<string>();
                        lst1.Add(height);
                        lst2.Add(width);
                    }
                }
                HashSet<int> hs1 = new HashSet<int>(lst1);
                HashSet<int> hs2 = new HashSet<int>(lst2);
                comboBox1.DataSource = hs1.ToList();
                comboBox2.DataSource = hs2.ToList();
            }

    Result of my test:

    Hope it can help you.

    Best Regards,

    Xingyu Zhao



    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, October 7, 2019 9:34 AM
    Moderator