I'm trying to retrieve values that are stored in a JSON string that I obtain from a website using Newtonsoft.Json.
I have the code below, but I cant work out how to loop through the data to retrieve the Name tag within the Palette->Threads->Name section. The number of thread names may vary from 1 to 15.
The desired outcome of the code below would be to output something like Colours Used: Black, Light Blue, White etc
Any help would be much appreciated, I've racked my brains looking at other peoples examples, but I've had no luck in applying Dictionary or Lists (I'm still learning .net)
protected void Page_Load(object sender, EventArgs e)
{
string jsondata = "{\"Width\":295,\"Height\":329,\"NumStitches\":1596,\"NumTrims\":1,\"Left\":479,\"Top\":-868,\"Right\":775,\"Bottom\":-539,\"Recipe\":\"Normal\",\"MachineFormat\":\"Tajima\",\"MasterDensity\":40,\"Palette\":{\"Threads\":[{\"Name\":\"Black\",\"Manufacturer\":\"Madeira 40\",\"Code\":\"1000\",\"Red\":0,\"Green\":0,\"Blue\":0,\"Type\":\"ttRayon\",\"Thickness\":3},{\"Name\":\"Light Blue\",\"Manufacturer\":\"Madeira 40\",\"Code\":\"1029\",\"Red\":0,\"Green\":114,\"Blue\":207,\"Type\":\"ttRayon\",\"Thickness\":3},{\"Name\":\"White\",\"Manufacturer\":\"Madeira 40\",\"Code\":\"1001\",\"Red\":255,\"Green\":255,\"Blue\":255,\"Type\":\"ttRayon\",\"Thickness\":3},{\"Name\":\"Mustard Brown\",\"Manufacturer\":\"Madeira 40\",\"Code\":\"1165\",\"Red\":255,\"Green\":153,\"Blue\":51,\"Type\":\"ttCotton\",\"Thickness\":3},{\"Name\":\"Midnight Blue\",\"Manufacturer\":\"Madeira 40\",\"Code\":\"1242\",\"Red\":0,\"Green\":40,\"Blue\":120,\"Type\":\"ttCotton\",\"Thickness\":3},{\"Name\":\"Jungle Green\",\"Manufacturer\":\"Madeira 40\",\"Code\":\"1249\",\"Red\":0,\"Green\":204,\"Blue\":0,\"Type\":\"ttCotton\",\"Thickness\":3},{\"Name\":\"Robin Egg Blue\",\"Manufacturer\":\"Madeira 40\",\"Code\":\"1093\",\"Red\":0,\"Green\":255,\"Blue\":255,\"Type\":\"ttCotton\",\"Thickness\":3},{\"Name\":\"Hyacinth\",\"Manufacturer\":\"Madeira 40\",\"Code\":\"1112\",\"Red\":125,\"Green\":0,\"Blue\":153,\"Type\":\"ttCotton\",\"Thickness\":3},{\"Name\":\"Aztec Gold\",\"Manufacturer\":\"Madeira 40\",\"Code\":\"1125\",\"Red\":255,\"Green\":240,\"Blue\":51,\"Type\":\"ttCotton\",\"Thickness\":3},{\"Name\":\"Evergreen\",\"Manufacturer\":\"Madeira 40\",\"Code\":\"1303\",\"Red\":0,\"Green\":73,\"Blue\":51,\"Type\":\"ttCotton\",\"Thickness\":3},{\"Name\":\"Lilac\",\"Manufacturer\":\"Madeira 40\",\"Code\":\"1033\",\"Red\":153,\"Green\":0,\"Blue\":153,\"Type\":\"ttCotton\",\"Thickness\":3},{\"Name\":\"Jet Black\",\"Manufacturer\":\"Madeira 40\",\"Code\":\"1000\",\"Red\":0,\"Green\":0,\"Blue\":0,\"Type\":\"ttCotton\",\"Thickness\":3},{\"Name\":\"Sapphire\",\"Manufacturer\":\"Madeira 40\",\"Code\":\"1076\",\"Red\":0,\"Green\":87,\"Blue\":150,\"Type\":\"ttCotton\",\"Thickness\":3},{\"Name\":\"Bordeaux\",\"Manufacturer\":\"Madeira 40\",\"Code\":\"1035\",\"Red\":99,\"Green\":47,\"Blue\":61,\"Type\":\"ttCotton\",\"Thickness\":3},{\"Name\":\"Flesh\",\"Manufacturer\":\"Madeira 40\",\"Code\":\"1017\",\"Red\":244,\"Green\":188,\"Blue\":172,\"Type\":\"ttCotton\",\"Thickness\":3}]},\"Needles\":[1,2,3]}";
var output = JsonConvert.DeserializeObject<jsonclass>(jsondata);
int StitchCount = output.NumStitches;
int StitchHeight = output.Height;
int StitchWidth = output.Width;
var pal = output.Palette;
// The following code is wrong, but illustrates what I'm trying to do
Response.Write("Colours used: ");
foreach (thread in pal["Threads"])
{
Response.Write(thread["Name"] & ",");
}
}
public class jsonclass
{
public int Width { get; set; }
public int Height { get; set; }
public int NumStitches { get; set; }
public Object Palette { get; set; }
}
I've stripped it down a bit, but this is a visual representation of the JSON data to help understand it
{
"Width":295,
"Height":329,
"NumStitches":1596,
"Palette":
{
"Threads":
[
{
"Name":"Black",
"Manufacturer":"Madeira 40",
"Code":"1000",
"Red":0,
"Green":0,
"Blue":0,
"Type":"ttRayon",
"Thickness":3
},
{
"Name":"Light Blue",
"Manufacturer":"Madeira 40",
"Code":"1029",
"Red":0,
"Green":114,
"Blue":207,
"Type":"ttRayon",
"Thickness":3
},
{
"Name":"White",
"Manufacturer":"Madeira 40",
"Code":"1001",
"Red":255,
"Green":255,
"Blue":255,
"Type":"ttRayon",
"Thickness":3
}
]},
"Needles":[1,2,3]
}
public dynamic Palette { get; set; }You will be able to access any property in runtime.