none
How to parse JSON in C# and find only certain parts RRS feed

  • Question

  • I have a file I got from a site that lists all textures and all names. I want to parse the JSON file and get just the name and texture parts get the url property and download the link with the name above.

    I'm not sure how to get this because the whole file is large but I just want to get the url and the width and height. If they are 2048 or 4042. If they match get the name and download the URL and give the name value to the name of download.

    So in summary 

    "name": "Metal_2_roughness.jpg",
    "images": [
        {
            "height": 2048,
            "createdAt": "2017-08-11T16:14:06.933292",
            "updatedAt": "2017-08-11T16:14:06.933307",
            "uid": "ae19ea6db7074248ac4e04b4db971913",
            "width": 2048,
            "options": {},
            "url": "https://media.sketchfab.com/urls/cbfbf4d275c24eafa51eaf3a6c3c91b9/dist/textures/16fb95f74f4846079a32f15c2be35565/ae19ea6db7074248ac4e04b4db971913.jpeg",
            "size": 1042057
        },
    
    

    If the height and width matches 2048 then check the name. Copy that then copy url and download the URL and give it the name property. But I'm not sure how to go about doing this as I never touched on JSON before. 

     https://paste.md-5.net/vecaxacafe.bash

    this is the full file 

    full json file

    the rest of the json file is not importent all i need name hight with to match 2048 x 2048 if it does then download the url thats under it as shown above example code but as you can tell i by the full json file the file is huge and to parse it is not working well i keep getting 

    errors like this 

    Newtonsoft.Json.JsonSerializationException
      HResult=0x80131500
      Message=Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Drawing.Image' because the type requires a JSON string value to deserialize correctly.
    To fix this error either change the JSON to a JSON string value or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List<T>) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.
    Path '['/i/models/5c62d2818a404977a6a38057c487a1ed']', line 2, position 53.
      Source=Newtonsoft.Json
      StackTrace:
       at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
       at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
       at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent)
       at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType)
       at Newtonsoft.Json.JsonConvert.DeserializeObject(String value, Type type, JsonSerializerSettings settings)
       at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value, JsonSerializerSettings settings)
       at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value)
       at sf_downloader.Form1.parsetest() in C:\Users\elfenliedtopfan5\source\repos\sf_downloader\Form1.cs:line 109
       at sf_downloader.Form1.SimpleButton2_Click(Object sender, EventArgs e) in C:\Users\elfenliedtopfan5\source\repos\sf_downloader\Form1.cs:line 99
       at System.Windows.Forms.Control.OnClick(EventArgs e)
       at DevExpress.XtraEditors.BaseButton.OnClick(EventArgs e)
       at DevExpress.XtraEditors.BaseButton.OnMouseUp(MouseEventArgs e)
       at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at DevExpress.Utils.Controls.ControlBase.WndProc(Message& m)
       at DevExpress.XtraEditors.BaseControl.WndProc(Message& msg)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.Run(Form mainForm)
       at sf_downloader.Program.Main() in C:\Users\elfenliedtopfan5\source\repos\sf_downloader\Program.cs:line 19

    any help would be much appeicated thanks in advance elfenliedtopfan5

    Saturday, September 7, 2019 12:21 AM

Answers

  • Hi elfenliedtopfan2, 

    Thank you for posting here.

    I make a test on my side, and I get no exception.

    My code:

                string path = @"Json File";
                var jsonContent = File.ReadAllText(path);
                JObject data = (JObject)JsonConvert.DeserializeObject(jsonContent);
    
                string texturesNodeKey = "/i/models/4ed3a36438974b25b4c863b4baf0a0b9/textures?optimized=1";
                List<dynamic> images = new List<dynamic>();
                foreach (var resultNode in data[texturesNodeKey]["results"])
                {
                    foreach (var imageNode in resultNode["images"])
                    {
                        var height = imageNode["height"].ToObject<long>();
                        var width = imageNode["width"].ToObject<long>();
                        if (height == 2048 && width == 2048)
                        {
                            images.Add(imageNode);
                        }
                    }
                }
                Console.WriteLine(images.Count);
                Console.ReadKey();

    jsonContent in my test:

    Result:

    I suggest you check the path in ‘File.ReadAllText(Path)’ and make sure the ‘jsonContent’ string is correct.

    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, September 9, 2019 8:12 AM
    Moderator

All replies

  •             JObject myObj = (JObject)JsonConvert.DeserializeObject(Application.StartupPath + "\\jsonfile.json");
                foreach (var resource in myObj["results"])
                {
                    var props = resource.Children<JObject>().First();
                    Console.WriteLine(props["name"] + " " + props["height"]["width"]["url"]);
                }
    code i am trying to use is here.
    Saturday, September 7, 2019 2:05 PM
  • Hi elfenliedtopfan2,

    Your JSON file not getting downloaded. Please share this file. 

    Are you trying to deserialize as Image? "Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Drawing.Image'".


    Avik Das

    Saturday, September 7, 2019 9:25 PM
  • Hi elfenliedtopfan2,

    Your JSON file not getting downloaded. Please share this file. 

    Are you trying to deserialize as Image? "Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Drawing.Image'".


    Avik Das

    the issue im having is this 
    https://hastebin.com/hagazevezo.jsonthis is the file in hastebin i only need to get 

     "/i/models/4ed3a36438974b25b4c863b4baf0a0b9/textures?optimized=1": {
            "results": [
                {
                    "name": "Barrelstrike_normal.png",
                    "colorSpace": "unknown",
                    "updatedAt": "2018-11-27T08:38:22.339671",
                    "uid": "69bab40ce1d546ee81c1b91747b8cf6c",
                    "createdAt": "2018-11-27T08:38:17.183365",
                    "images": [
                        {
                            "size": 2268,
                            "width": 32,
                            "url": "https://media.sketchfab.com/urls/4ed3a36438974b25b4c863b4baf0a0b9/dist/textures/69bab40ce1d546ee81c1b91747b8cf6c/3e71ddc7cdb340379cad2521c508d2d8.png",
                            "updatedAt": "2018-11-27T08:38:22.292490",
                            "uid": "3e71ddc7cdb340379cad2521c508d2d8",
                            "height": 32,
                            "createdAt": "2018-11-27T08:38:22.324799",
                            "options": {}
                        },
                        {
                            "size": 14185286,
                            "width": 4096,
                            "url": "https://media.sketchfab.com/urls/4ed3a36438974b25b4c863b4baf0a0b9/dist/textures/69bab40ce1d546ee81c1b91747b8cf6c/28f5be5293f04af6806b27f223fbed06.png",
                            "updatedAt": "2018-11-27T08:38:17.186941",
                            "uid": "28f5be5293f04af6806b27f223fbed06",
                            "height": 4096,
                            "createdAt": "2018-11-27T08:38:18.460950",
                            "options": {}
                        },
                        {
                            "size": 4705549,
                            "width": 4096,
                            "url": "https://media.sketchfab.com/urls/4ed3a36438974b25b4c863b4baf0a0b9/dist/textures/69bab40ce1d546ee81c1b91747b8cf6c/6b65df6b6f274fe789026c01fab174db.jpeg",
                            "updatedAt": "2018-11-27T17:59:24.238940",
                            "uid": "6b65df6b6f274fe789026c01fab174db",
                            "height": 4096,
                            "createdAt": "2018-11-27T17:59:24.850142",
                            "options": {
                                "format": "N",
                                "quality": 92
                            }
                        },
                        {
                            "size": 1366363,
                            "width": 2048,
                            "url": "https://media.sketchfab.com/urls/4ed3a36438974b25b4c863b4baf0a0b9/dist/textures/69bab40ce1d546ee81c1b91747b8cf6c/22917eb565b043009f6ff6ca2999b12d.jpeg",
                            "updatedAt": "2018-11-27T17:59:24.856900",
                            "uid": "22917eb565b043009f6ff6ca2999b12d",
                            "height": 2048,
                            "createdAt": "2018-11-27T17:59:25.031565",
                            "options": {
                                "format": "N",
                                "quality": 92
                            }
                        },
                        {
                            "size": 457473,
                            "width": 1024,
                            "url": "https://media.sketchfab.com/urls/4ed3a36438974b25b4c863b4baf0a0b9/dist/textures/69bab40ce1d546ee81c1b91747b8cf6c/b08998ba0379489d91b032f7ec2e0798.jpeg",
                            "updatedAt": "2018-11-27T17:59:25.037397",
                            "uid": "b08998ba0379489d91b032f7ec2e0798",
                            "height": 1024,
                            "createdAt": "2018-11-27T17:59:25.110159",
                            "options": {
                                "format": "N",
                                "quality": 92
                            }
                        },
                        {
                            "size": 199101,
                            "width": 512,
                            "url": "https://media.sketchfab.com/urls/4ed3a36438974b25b4c863b4baf0a0b9/dist/textures/69bab40ce1d546ee81c1b91747b8cf6c/ba7a0c1f19d745c9b602117d8c6b8b57.jpeg",
                            "updatedAt": "2018-11-27T17:59:25.115940",
                            "uid": "ba7a0c1f19d745c9b602117d8c6b8b57",
                            "height": 512,
                            "createdAt": "2018-11-27T17:59:25.205599",
                            "options": {
                                "format": "N",
                                "quality": 95
                            }
                        },
                        {
                            "size": 64703,
                            "width": 256,
                            "url": "https://media.sketchfab.com/urls/4ed3a36438974b25b4c863b4baf0a0b9/dist/textures/69bab40ce1d546ee81c1b91747b8cf6c/9e6f5c25f22647e6972e908d5c4b6c2d.jpeg",
                            "updatedAt": "2018-11-27T17:59:25.210802",
                            "uid": "9e6f5c25f22647e6972e908d5c4b6c2d",
                            "height": 256,
                            "createdAt": "2018-11-27T17:59:25.269286",
                            "options": {
                                "format": "N",
                                "quality": 95
                            }
                        },
                        {
                            "size": 18912,
                            "width": 128,
                            "url": "https://media.sketchfab.com/urls/4ed3a36438974b25b4c863b4baf0a0b9/dist/textures/69bab40ce1d546ee81c1b91747b8cf6c/901e611c71fc410fba39299f3b148b85.jpeg",
                            "updatedAt": "2018-11-27T17:59:25.274229",
                            "uid": "901e611c71fc410fba39299f3b148b85",
                            "height": 128,
                            "createdAt": "2018-11-27T17:59:25.316088",
                            "options": {
                                "format": "N",
                                "quality": 95
                            }
                        },
                        {
                            "size": 5094,
                            "width": 64,
                            "url": "https://media.sketchfab.com/urls/4ed3a36438974b25b4c863b4baf0a0b9/dist/textures/69bab40ce1d546ee81c1b91747b8cf6c/8fa844a08d284d2ab95aa80c2fb658ab.jpeg",
                            "updatedAt": "2018-11-27T17:59:25.321375",
                            "uid": "8fa844a08d284d2ab95aa80c2fb658ab",
                            "height": 64,
                            "createdAt": "2018-11-27T17:59:25.443556",
                            "options": {
                                "format": "N",
                                "quality": 95
                            }
                        }
                    ]
                },
                {
                    "name": "Barrelstrike_albedo.jpg",
                    "colorSpace": "unknown",
                    "updatedAt": "2018-11-27T08:38:11.406860",
                    "uid": "99bb9021d54c4d1db676aa2c3e4a465c",
                    "createdAt": "2018-11-27T08:38:07.735965",
                    "images": [
                        {
                            "size": 2823269,
                            "width": 4096,
                            "url": "https://media.sketchfab.com/urls/4ed3a36438974b25b4c863b4baf0a0b9/dist/textures/99bb9021d54c4d1db676aa2c3e4a465c/7e78a96dd30b476dbdfb9a637ef70ed5.jpg",
                            "updatedAt": "2018-11-27T08:38:07.775199",
                            "uid": "7e78a96dd30b476dbdfb9a637ef70ed5",
                            "height": 4096,
                            "createdAt": "2018-11-27T08:38:07.917213",
                            "options": {}
                        },
                        {
                            "size": 2142,
                            "width": 32,
                            "url": "https://media.sketchfab.com/urls/4ed3a36438974b25b4c863b4baf0a0b9/dist/textures/99bb9021d54c4d1db676aa2c3e4a465c/b76c4a2c41e04b92b6b5b5209af373ba.jpeg",
                            "updatedAt": "2018-11-27T08:38:11.347369",
                            "uid": "b76c4a2c41e04b92b6b5b5209af373ba",
                            "height": 32,
                            "createdAt": "2018-11-27T08:38:11.394890",
                            "options": {}
                        },
                        {
                            "size": 2638982,
                            "width": 4096,
                            "url": "https://media.sketchfab.com/urls/4ed3a36438974b25b4c863b4baf0a0b9/dist/textures/99bb9021d54c4d1db676aa2c3e4a465c/8b2f71b883e94509b43fc3cd3ba045d9.jpeg",
                            "updatedAt": "2018-11-27T17:59:41.982118",
                            "uid": "8b2f71b883e94509b43fc3cd3ba045d9",
                            "height": 4096,
                            "createdAt": "2018-11-27T17:59:42.104240",
                            "options": {
                                "format": "RGB",
                                "quality": 88
                            }
                        },
                        {
                            "size": 718632,
                            "width": 2048,
                            "url": "https://media.sketchfab.com/urls/4ed3a36438974b25b4c863b4baf0a0b9/dist/textures/99bb9021d54c4d1db676aa2c3e4a465c/5490564eacbf48e481b91b0af977014f.jpeg",
                            "updatedAt": "2018-11-27T17:59:42.111117",
                            "uid": "5490564eacbf48e481b91b0af977014f",
                            "height": 2048,
                            "createdAt": "2018-11-27T17:59:42.169635",
                            "options": {
                                "format": "RGB",
                                "quality": 88
                            }
                        },
                        {
                            "size": 335413,
                            "width": 1024,
                            "url": "https://media.sketchfab.com/urls/4ed3a36438974b25b4c863b4baf0a0b9/dist/textures/99bb9021d54c4d1db676aa2c3e4a465c/69742546ef074c9baeddd7080f60ced1.jpeg",
                            "updatedAt": "2018-11-27T17:59:42.175223",
                            "uid": "69742546ef074c9baeddd7080f60ced1",
                            "height": 1024,
                            "createdAt": "2018-11-27T17:59:42.294973",
                            "options": {
                                "format": "RGB",
                                "quality": 92
                            }
                        },
                        {
                            "size": 138778,
                            "width": 512,
                            "url": "https://media.sketchfab.com/urls/4ed3a36438974b25b4c863b4baf0a0b9/dist/textures/99bb9021d54c4d1db676aa2c3e4a465c/fd77b10461a746858412906b42231dcc.jpeg",
                            "updatedAt": "2018-11-27T17:59:42.300391",
                            "uid": "fd77b10461a746858412906b42231dcc",
                            "height": 512,
                            "createdAt": "2018-11-27T17:59:42.373102",
                            "options": {
                                "format": "RGB",
                                "quality": 95
                            }
                        },
                        {
                            "size": 44141,
                            "width": 256,
                            "url": "https://media.sketchfab.com/urls/4ed3a36438974b25b4c863b4baf0a0b9/dist/textures/99bb9021d54c4d1db676aa2c3e4a465c/5c3a4d8a13c34f30861e434b4bfad482.jpeg",
                            "updatedAt": "2018-11-27T17:59:42.379167",
                            "uid": "5c3a4d8a13c34f30861e434b4bfad482",
                            "height": 256,
                            "createdAt": "2018-11-27T17:59:42.435116",
                            "options": {
                                "format": "RGB",
                                "quality": 95
                            }
                        },
                        {
                            "size": 13466,
                            "width": 128,
                            "url": "https://media.sketchfab.com/urls/4ed3a36438974b25b4c863b4baf0a0b9/dist/textures/99bb9021d54c4d1db676aa2c3e4a465c/c8bc0e40f5e1452da879b2cad2b0336d.jpeg",
                            "updatedAt": "2018-11-27T17:59:42.440545",
                            "uid": "c8bc0e40f5e1452da879b2cad2b0336d",
                            "height": 128,
                            "createdAt": "2018-11-27T17:59:42.494045",
                            "options": {
                                "format": "RGB",
                                "quality": 95
                            }
                        },
                        {
                            "size": 3900,
                            "width": 64,
                            "url": "https://media.sketchfab.com/urls/4ed3a36438974b25b4c863b4baf0a0b9/dist/textures/99bb9021d54c4d1db676aa2c3e4a465c/bd832c814cdb4938834749d050f12f54.jpeg",
                            "updatedAt": "2018-11-27T17:59:42.499300",
                            "uid": "bd832c814cdb4938834749d050f12f54",
                            "height": 64,
                            "createdAt": "2018-11-27T17:59:42.531932",
                            "options": {
                                "format": "RGB",
                                "quality": 95
                            }
                        }
                    ]
                },
                {
                    "name": "Barrelstrike_metallic.jpg",
                    "colorSpace": "unknown",
                    "updatedAt": "2018-11-27T08:38:12.222382",
                    "uid": "9dd4d6ce77444ffab6da0c6da2e0c597",
                    "createdAt": "2018-11-27T08:38:07.225362",
                    "images": [
                        {
                            "size": 1278,
                            "width": 32,
                            "url": "https://media.sketchfab.com/urls/4ed3a36438974b25b4c863b4baf0a0b9/dist/textures/9dd4d6ce77444ffab6da0c6da2e0c597/fa86bce310834c7e885a6534b203f576.jpeg",
                            "updatedAt": "2018-11-27T08:38:12.176367",
                            "uid": "fa86bce310834c7e885a6534b203f576",
                            "height": 32,
                            "createdAt": "2018-11-27T08:38:12.211797",
                            "options": {}
                        },
                        {
                            "size": 2663501,
                            "width": 4096,
                            "url": "https://media.sketchfab.com/urls/4ed3a36438974b25b4c863b4baf0a0b9/dist/textures/9dd4d6ce77444ffab6da0c6da2e0c597/4aae9dc67a0d4a54a03cbddbe659e6f4.jpg",
                            "updatedAt": "2018-11-27T08:38:07.269089",
                            "uid": "4aae9dc67a0d4a54a03cbddbe659e6f4",
                            "height": 4096,
                            "createdAt": "2018-11-27T08:38:08.114551",
                            "options": {}
                        },
                        {
                            "size": 2381380,
                            "width": 4096,
                            "url": "https://media.sketchfab.com/urls/4ed3a36438974b25b4c863b4baf0a0b9/dist/textures/9dd4d6ce77444ffab6da0c6da2e0c597/bb3e2eb8b854445e803f398cc373e291.jpeg",
                            "updatedAt": "2018-11-27T17:58:57.972302",
                            "uid": "bb3e2eb8b854445e803f398cc373e291",
                            "height": 4096,
                            "createdAt": "2018-11-27T17:58:59.145787",
                            "options": {
                                "format": "R",
                                "quality": 88
                            }
                        },
                        {
                            "size": 763725,
                            "width": 2048,
                            "url": "https://media.sketchfab.com/urls/4ed3a36438974b25b4c863b4baf0a0b9/dist/textures/9dd4d6ce77444ffab6da0c6da2e0c597/199d233b34244411a07d6bd268e770d4.jpeg",
                            "updatedAt": "2018-11-27T17:58:59.152832",
                            "uid": "199d233b34244411a07d6bd268e770d4",
                            "height": 2048,
                            "createdAt": "2018-11-27T17:58:59.219430",
                            "options": {
                                "format": "R",
                                "quality": 88
                            }
                        },
                        {
                            "size": 308686,
                            "width": 1024,
                            "url": "https://media.sketchfab.com/urls/4ed3a36438974b25b4c863b4baf0a0b9/dist/textures/9dd4d6ce77444ffab6da0c6da2e0c597/79a044e655144390916d5397465580e2.jpeg",
                            "updatedAt": "2018-11-27T17:58:59.225018",
                            "uid": "79a044e655144390916d5397465580e2",
                            "height": 1024,
                            "createdAt": "2018-11-27T17:58:59.387784",
                            "options": {
                                "format": "R",
                                "quality": 92
                            }
                        },
                        {
                            "size": 122536,
                            "width": 512,
                            "url": "https://media.sketchfab.com/urls/4ed3a36438974b25b4c863b4baf0a0b9/dist/textures/9dd4d6ce77444ffab6da0c6da2e0c597/ac615ba856314e4787964753b1c430fa.jpeg",
                            "updatedAt": "2018-11-27T17:58:59.394466",
                            "uid": "ac615ba856314e4787964753b1c430fa",
                            "height": 512,
                            "createdAt": "2018-11-27T17:58:59.431226",
                            "options": {
                                "format": "R",
                                "quality": 95
                            }
                        },
                        {
                            "size": 38609,
                            "width": 256,
                            "url": "https://media.sketchfab.com/urls/4ed3a36438974b25b4c863b4baf0a0b9/dist/textures/9dd4d6ce77444ffab6da0c6da2e0c597/fd5fea29f9444bf0910ac3f1d2ee5507.jpeg",
                            "updatedAt": "2018-11-27T17:58:59.436377",
                            "uid": "fd5fea29f9444bf0910ac3f1d2ee5507",
                            "height": 256,
                            "createdAt": "2018-11-27T17:58:59.493803",
                            "options": {
                                "format": "R",
                                "quality": 95
                            }
                        },
                        {
                            "size": 11418,
                            "width": 128,
                            "url": "https://media.sketchfab.com/urls/4ed3a36438974b25b4c863b4baf0a0b9/dist/textures/9dd4d6ce77444ffab6da0c6da2e0c597/83a439a6d8e441daa15e4450dc4f150f.jpeg",
                            "updatedAt": "2018-11-27T17:58:59.500035",
                            "uid": "83a439a6d8e441daa15e4450dc4f150f",
                            "height": 128,
                            "createdAt": "2018-11-27T17:58:59.569661",
                            "options": {
                                "format": "R",
                                "quality": 95
                            }
                        },
                        {
                            "size": 3185,
                            "width": 64,
                            "url": "https://media.sketchfab.com/urls/4ed3a36438974b25b4c863b4baf0a0b9/dist/textures/9dd4d6ce77444ffab6da0c6da2e0c597/c366f7cc1070403c92c061a095217f08.jpeg",
                            "updatedAt": "2018-11-27T17:58:59.574881",
                            "uid": "c366f7cc1070403c92c061a095217f08",
                            "height": 64,
                            "createdAt": "2018-11-27T17:58:59.618398",
                            "options": {
                                "format": "R",
                                "quality": 95
                            }
                        }
                    ]
                },
                {
                    "name": "Barrelstrike_roughness.jpg",
                    "colorSpace": "unknown",
                    "updatedAt": "2018-11-27T08:38:11.199911",
                    "uid": "cdacf0cbfb90458ab37dd5dd2cf401d4",
                    "createdAt": "2018-11-27T08:38:07.115729",
                    "images": [
                        {
                            "size": 2372268,
                            "width": 4096,
                            "url": "https://media.sketchfab.com/urls/4ed3a36438974b25b4c863b4baf0a0b9/dist/textures/cdacf0cbfb90458ab37dd5dd2cf401d4/5706085bbf294ab6ac07392a8ca89641.jpg",
                            "updatedAt": "2018-11-27T08:38:07.127727",
                            "uid": "5706085bbf294ab6ac07392a8ca89641",
                            "height": 4096,
                            "createdAt": "2018-11-27T08:38:07.228141",
                            "options": {}
                        },
                        {
                            "size": 1175,
                            "width": 32,
                            "url": "https://media.sketchfab.com/urls/4ed3a36438974b25b4c863b4baf0a0b9/dist/textures/cdacf0cbfb90458ab37dd5dd2cf401d4/30f7b69c04de4a229ff911a77083c42b.jpeg",
                            "updatedAt": "2018-11-27T08:38:11.090243",
                            "uid": "30f7b69c04de4a229ff911a77083c42b",
                            "height": 32,
                            "createdAt": "2018-11-27T08:38:11.189740",
                            "options": {}
                        },
                        {
                            "size": 2153815,
                            "width": 4096,
                            "url": "https://media.sketchfab.com/urls/4ed3a36438974b25b4c863b4baf0a0b9/dist/textures/cdacf0cbfb90458ab37dd5dd2cf401d4/e0a7da87d25346e5b417b6dfedb5e1b0.jpeg",
                            "updatedAt": "2018-11-27T17:59:01.775867",
                            "uid": "e0a7da87d25346e5b417b6dfedb5e1b0",
                            "height": 4096,
                            "createdAt": "2018-11-27T17:59:02.538490",
                            "options": {
                                "format": "R",
                                "quality": 88
                            }
                        },
                        {
                            "size": 622785,
                            "width": 2048,
                            "url": "https://media.sketchfab.com/urls/4ed3a36438974b25b4c863b4baf0a0b9/dist/textures/cdacf0cbfb90458ab37dd5dd2cf401d4/2330fd36cdda4509a1f5632c2936701a.jpeg",
                            "updatedAt": "2018-11-27T17:59:02.544055",
                            "uid": "2330fd36cdda4509a1f5632c2936701a",
                            "height": 2048,
                            "createdAt": "2018-11-27T17:59:02.695429",
                            "options": {
                                "format": "R",
                                "quality": 88
                            }
                        },
                        {
                            "size": 238328,
                            "width": 1024,
                            "url": "https://media.sketchfab.com/urls/4ed3a36438974b25b4c863b4baf0a0b9/dist/textures/cdacf0cbfb90458ab37dd5dd2cf401d4/df7db6bb4f79426499408cb3ca771017.jpeg",
                            "updatedAt": "2018-11-27T17:59:02.699751",
                            "uid": "df7db6bb4f79426499408cb3ca771017",
                            "height": 1024,
                            "createdAt": "2018-11-27T17:59:02.814495",
                            "options": {
                                "format": "R",
                                "quality": 92
                            }
                        },
                        {
                            "size": 96056,
                            "width": 512,
                            "url": "https://media.sketchfab.com/urls/4ed3a36438974b25b4c863b4baf0a0b9/dist/textures/cdacf0cbfb90458ab37dd5dd2cf401d4/a9fbd3373f2649b7ba805a1b3d891416.jpeg",
                            "updatedAt": "2018-11-27T17:59:02.818825",
                            "uid": "a9fbd3373f2649b7ba805a1b3d891416",
                            "height": 512,
                            "createdAt": "2018-11-27T17:59:02.913804",
                            "options": {
                                "format": "R",
                                "quality": 95
                            }
                        },
                        {
                            "size": 30401,
                            "width": 256,
                            "url": "https://media.sketchfab.com/urls/4ed3a36438974b25b4c863b4baf0a0b9/dist/textures/cdacf0cbfb90458ab37dd5dd2cf401d4/65875f2ef8014cc989da26c1b94104c7.jpeg",
                            "updatedAt": "2018-11-27T17:59:02.919592",
                            "uid": "65875f2ef8014cc989da26c1b94104c7",
                            "height": 256,
                            "createdAt": "2018-11-27T17:59:03.108696",
                            "options": {
                                "format": "R",
                                "quality": 95
                            }
                        },
                        {
                            "size": 9265,
                            "width": 128,
                            "url": "https://media.sketchfab.com/urls/4ed3a36438974b25b4c863b4baf0a0b9/dist/textures/cdacf0cbfb90458ab37dd5dd2cf401d4/a673e0e9896a43dfb266e80831a74a6f.jpeg",
                            "updatedAt": "2018-11-27T17:59:03.112943",
                            "uid": "a673e0e9896a43dfb266e80831a74a6f",
                            "height": 128,
                            "createdAt": "2018-11-27T17:59:03.193761",
                            "options": {
                                "format": "R",
                                "quality": 95
                            }
                        },
                        {
                            "size": 2658,
                            "width": 64,
                            "url": "https://media.sketchfab.com/urls/4ed3a36438974b25b4c863b4baf0a0b9/dist/textures/cdacf0cbfb90458ab37dd5dd2cf401d4/8bdda190646f414e986f07e48d8324ae.jpeg",
                            "updatedAt": "2018-11-27T17:59:03.198074",
                            "uid": "8bdda190646f414e986f07e48d8324ae",
                            "height": 64,
                            "createdAt": "2018-11-27T17:59:03.312068",
                            "options": {
                                "format": "R",
                                "quality": 95
                            }
                        }
                    ]
                },
                {
                    "name": "internal_ground_ao_texture.jpeg",
                    "colorSpace": "unknown",
                    "updatedAt": "2018-11-27T08:38:08.020870",
                    "uid": "e0bd9fea70404053a5bf45e925d50ec5",
                    "createdAt": "2018-11-27T08:38:04.532332",
                    "images": [
                        {
                            "size": 16283,
                            "width": 512,
                            "url": "https://media.sketchfab.com/urls/4ed3a36438974b25b4c863b4baf0a0b9/dist/textures/e0bd9fea70404053a5bf45e925d50ec5/f32452b0bf6a414da1c90c13a94b13bc.jpeg",
                            "updatedAt": "2018-11-27T08:38:04.776425",
                            "uid": "f32452b0bf6a414da1c90c13a94b13bc",
                            "height": 512,
                            "createdAt": "2018-11-27T08:38:05.800547",
                            "options": {}
                        },
                        {
                            "size": 653,
                            "width": 32,
                            "url": "https://media.sketchfab.com/urls/4ed3a36438974b25b4c863b4baf0a0b9/dist/textures/e0bd9fea70404053a5bf45e925d50ec5/947c2105bebf4b7f8df3711b67918759.jpeg",
                            "updatedAt": "2018-11-27T08:38:07.738160",
                            "uid": "947c2105bebf4b7f8df3711b67918759",
                            "height": 32,
                            "createdAt": "2018-11-27T08:38:07.861600",
                            "options": {}
                        },
                        {
                            "size": 18054,
                            "width": 512,
                            "url": "https://media.sketchfab.com/urls/4ed3a36438974b25b4c863b4baf0a0b9/dist/textures/e0bd9fea70404053a5bf45e925d50ec5/b87ff65de67b4c8180dbaf7d5500fe8c.jpeg",
                            "updatedAt": "2018-11-27T17:59:00.800914",
                            "uid": "b87ff65de67b4c8180dbaf7d5500fe8c",
                            "height": 512,
                            "createdAt": "2018-11-27T17:59:00.882508",
                            "options": {
                                "format": "RGB",
                                "quality": 95
                            }
                        },
                        {
                            "size": 6196,
                            "width": 256,
                            "url": "https://media.sketchfab.com/urls/4ed3a36438974b25b4c863b4baf0a0b9/dist/textures/e0bd9fea70404053a5bf45e925d50ec5/7029d641d82f4cc2ba97a2f9d0ff51ef.jpeg",
                            "updatedAt": "2018-11-27T17:59:00.888683",
                            "uid": "7029d641d82f4cc2ba97a2f9d0ff51ef",
                            "height": 256,
                            "createdAt": "2018-11-27T17:59:00.946098",
                            "options": {
                                "format": "RGB",
                                "quality": 95
                            }
                        },
                        {
                            "size": 2114,
                            "width": 128,
                            "url": "https://media.sketchfab.com/urls/4ed3a36438974b25b4c863b4baf0a0b9/dist/textures/e0bd9fea70404053a5bf45e925d50ec5/a876f59080fd456bb1a164e4982ae43c.jpeg",
                            "updatedAt": "2018-11-27T17:59:00.951192",
                            "uid": "a876f59080fd456bb1a164e4982ae43c",
                            "height": 128,
                            "createdAt": "2018-11-27T17:59:01.053014",
                            "options": {
                                "format": "RGB",
                                "quality": 95
                            }
                        },
                        {
                            "size": 839,
                            "width": 64,
                            "url": "https://media.sketchfab.com/urls/4ed3a36438974b25b4c863b4baf0a0b9/dist/textures/e0bd9fea70404053a5bf45e925d50ec5/ddc67a69596241eea2f983de72cb73e0.jpeg",
                            "updatedAt": "2018-11-27T17:59:01.062730",
                            "uid": "ddc67a69596241eea2f983de72cb73e0",
                            "height": 64,
                            "createdAt": "2018-11-27T17:59:01.097354",
                            "options": {
                                "format": "RGB",
                                "quality": 95
                            }
                        }
                    ]
                },
                {
                    "name": "Barrelstrike_AO.jpg",
                    "colorSpace": "unknown",
                    "updatedAt": "2018-11-27T08:38:08.912179",
                    "uid": "eebebe85040e4015a54bc58a58e19924",
                    "createdAt": "2018-11-27T08:38:06.501790",
                    "images": [
                        {
                            "size": 1876737,
                            "width": 4096,
                            "url": "https://media.sketchfab.com/urls/4ed3a36438974b25b4c863b4baf0a0b9/dist/textures/eebebe85040e4015a54bc58a58e19924/d976ff722f73459a9cb42e43e013773b.jpg",
                            "updatedAt": "2018-11-27T08:38:06.566824",
                            "uid": "d976ff722f73459a9cb42e43e013773b",
                            "height": 4096,
                            "createdAt": "2018-11-27T08:38:06.699631",
                            "options": {}
                        },
                        {
                            "size": 1239,
                            "width": 32,
                            "url": "https://media.sketchfab.com/urls/4ed3a36438974b25b4c863b4baf0a0b9/dist/textures/eebebe85040e4015a54bc58a58e19924/104306a61b69446fa3700ed02b8c65e8.jpeg",
                            "updatedAt": "2018-11-27T08:38:08.524271",
                            "uid": "104306a61b69446fa3700ed02b8c65e8",
                            "height": 32,
                            "createdAt": "2018-11-27T08:38:08.556088",
                            "options": {}
                        },
                        {
                            "size": 1630287,
                            "width": 4096,
                            "url": "https://media.sketchfab.com/urls/4ed3a36438974b25b4c863b4baf0a0b9/dist/textures/eebebe85040e4015a54bc58a58e19924/48115a782b2a48fcbf3019f3dd7dfbb6.jpeg",
                            "updatedAt": "2018-11-27T17:59:10.694258",
                            "uid": "48115a782b2a48fcbf3019f3dd7dfbb6",
                            "height": 4096,
                            "createdAt": "2018-11-27T17:59:11.301588",
                            "options": {
                                "format": "R",
                                "quality": 88
                            }
                        },
                        {
                            "size": 548769,
                            "width": 2048,
                            "url": "https://media.sketchfab.com/urls/4ed3a36438974b25b4c863b4baf0a0b9/dist/textures/eebebe85040e4015a54bc58a58e19924/c8db4fd61dc94e3481545573070b0d76.jpeg",
                            "updatedAt": "2018-11-27T17:59:11.306588",
                            "uid": "c8db4fd61dc94e3481545573070b0d76",
                            "height": 2048,
                            "createdAt": "2018-11-27T17:59:11.370357",
                            "options": {
                                "format": "R",
                                "quality": 88
                            }
                        },
                        {
                            "size": 226118,
                            "width": 1024,
                            "url": "https://media.sketchfab.com/urls/4ed3a36438974b25b4c863b4baf0a0b9/dist/textures/eebebe85040e4015a54bc58a58e19924/1b89c1c5bac8483b94c3572fca0164f2.jpeg",
                            "updatedAt": "2018-11-27T17:59:11.376189",
                            "uid": "1b89c1c5bac8483b94c3572fca0164f2",
                            "height": 1024,
                            "createdAt": "2018-11-27T17:59:11.543981",
                            "options": {
                                "format": "R",
                                "quality": 92
                            }
                        },
                        {
                            "size": 95040,
                            "width": 512,
                            "url": "https://media.sketchfab.com/urls/4ed3a36438974b25b4c863b4baf0a0b9/dist/textures/eebebe85040e4015a54bc58a58e19924/b886149dbfda452cac8ff0ba6e183fe5.jpeg",
                            "updatedAt": "2018-11-27T17:59:11.548953",
                            "uid": "b886149dbfda452cac8ff0ba6e183fe5",
                            "height": 512,
                            "createdAt": "2018-11-27T17:59:11.631477",
                            "options": {
                                "format": "R",
                                "quality": 95
                            }
                        },
                        {
                            "size": 31008,
                            "width": 256,
                            "url": "https://media.sketchfab.com/urls/4ed3a36438974b25b4c863b4baf0a0b9/dist/textures/eebebe85040e4015a54bc58a58e19924/c995e963ee0f4fd49be7ee98122fd752.jpeg",
                            "updatedAt": "2018-11-27T17:59:11.636209",
                            "uid": "c995e963ee0f4fd49be7ee98122fd752",
                            "height": 256,
                            "createdAt": "2018-11-27T17:59:11.729777",
                            "options": {
                                "format": "R",
                                "quality": 95
                            }
                        },
                        {
                            "size": 9580,
                            "width": 128,
                            "url": "https://media.sketchfab.com/urls/4ed3a36438974b25b4c863b4baf0a0b9/dist/textures/eebebe85040e4015a54bc58a58e19924/bcea8e6aeffa465ab928c389efcb453b.jpeg",
                            "updatedAt": "2018-11-27T17:59:11.736089",
                            "uid": "bcea8e6aeffa465ab928c389efcb453b",
                            "height": 128,
                            "createdAt": "2018-11-27T17:59:11.788058",
                            "options": {
                                "format": "R",
                                "quality": 95
                            }
                        },
                        {
                            "size": 2853,
                            "width": 64,
                            "url": "https://media.sketchfab.com/urls/4ed3a36438974b25b4c863b4baf0a0b9/dist/textures/eebebe85040e4015a54bc58a58e19924/d28ad32b68844f0ea78a75a11d514804.jpeg",
                            "updatedAt": "2018-11-27T17:59:11.792454",
                            "uid": "d28ad32b68844f0ea78a75a11d514804",
                            "height": 64,
                            "createdAt": "2018-11-27T17:59:11.823163",
                            "options": {
                                "format": "R",
                                "quality": 95
                            }
                        }
                    ]
                }
            ],
            "count": 6
    };

    inside this and get all names and hight width and url of the hight and width matching 2048 get the name and the url download url and save image with the name name value 

    need to do it for all occrences in this tree that match 2048 there are diffent textures for each part of the model i need to get 

    Sunday, September 8, 2019 4:44 PM
  • First of all there is a minor mistake I observed in your code.

    JObject myObj = (JObject)JsonConvert.DeserializeObject(Application.StartupPath + "\\jsonfile.json");

    You are passing the file path instead of JSON string to DeserializeObject method. You need to read the content first then pass it to this method.

    var jsonContent = File.ReadAllText(@"<your file path>\input.json");
    var data = (JObject)JsonConvert.DeserializeObject(jsonContent);

    Now, you can simple use the following code to get only matching records and add into a list, or whatever you like to do with this data.

    var texturesNodeKey = "/i/models/4ed3a36438974b25b4c863b4baf0a0b9/textures?optimized=1";
    var images = new List<dynamic>();
    foreach (var resultNode in data[texturesNodeKey]["results"]) {
        foreach (var imageNode in resultNode["images"])
        {
            var height = imageNode["height"].ToObject<long>();
            var width = imageNode["width"].ToObject<long>();
            if (height == 2048 && width==2048)
            {
                images.Add(imageNode);
            }
        }
    }


    Avik Das


    • Edited by D Avik Sunday, September 8, 2019 7:10 PM
    Sunday, September 8, 2019 7:09 PM
  • First of all there is a minor mistake I observed in your code.

    JObject myObj = (JObject)JsonConvert.DeserializeObject(Application.StartupPath + "\\jsonfile.json");

    You are passing the file path instead of JSON string to DeserializeObject method. You need to read the content first then pass it to this method.

    var jsonContent = File.ReadAllText(@"<your file path>\input.json");
    var data = (JObject)JsonConvert.DeserializeObject(jsonContent);

    Now, you can simple use the following code to get only matching records and add into a list, or whatever you like to do with this data.

    var texturesNodeKey = "/i/models/4ed3a36438974b25b4c863b4baf0a0b9/textures?optimized=1";
    var images = new List<dynamic>();
    foreach (var resultNode in data[texturesNodeKey]["results"]) {
        foreach (var imageNode in resultNode["images"])
        {
            var height = imageNode["height"].ToObject<long>();
            var width = imageNode["width"].ToObject<long>();
            if (height == 2048 && width==2048)
            {
                images.Add(imageNode);
            }
        }
    }


    Avik Das


    i seem to get object revrence is not set to a instance of a object error 

    Sunday, September 8, 2019 8:11 PM
  • Hi elfenliedtopfan2, 

    Thank you for posting here.

    I make a test on my side, and I get no exception.

    My code:

                string path = @"Json File";
                var jsonContent = File.ReadAllText(path);
                JObject data = (JObject)JsonConvert.DeserializeObject(jsonContent);
    
                string texturesNodeKey = "/i/models/4ed3a36438974b25b4c863b4baf0a0b9/textures?optimized=1";
                List<dynamic> images = new List<dynamic>();
                foreach (var resultNode in data[texturesNodeKey]["results"])
                {
                    foreach (var imageNode in resultNode["images"])
                    {
                        var height = imageNode["height"].ToObject<long>();
                        var width = imageNode["width"].ToObject<long>();
                        if (height == 2048 && width == 2048)
                        {
                            images.Add(imageNode);
                        }
                    }
                }
                Console.WriteLine(images.Count);
                Console.ReadKey();

    jsonContent in my test:

    Result:

    I suggest you check the path in ‘File.ReadAllText(Path)’ and make sure the ‘jsonContent’ string is correct.

    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, September 9, 2019 8:12 AM
    Moderator
  • string texturesNodeKey = "/i/models/4ed3a36438974b25b4c863b4baf0a0b9/textures?optimized=1";
    The above value is not same in your current JSON file. Might be this key is no longer exists. Please check the 2nd line of data in your screenshot. Please update the new key in your code and try.

    Avik Das

    Monday, September 9, 2019 8:23 AM