Click here to Skip to main content
15,887,435 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

i have variable Json and want to deserialize with Newtonsoft.Json.
This type of Json will not be able to be transformed to a c# class. What to do?
Thanks in advance.

The "variableKeyxxx123" - keys can occur several times with random names.
In some times, there are "morevalues".


{
	"result": 
	{
		"computers": {
			"variableKeyxxx123": 
			{
				"id": 3333,
				"name": "name1",
				"xvalues": "xxValues"
			},
			"variableKeyxxx862": 
			{
				"id": 4444,
				"name": "name2",
				"xvalues": "xxValues"
			},
			"variableKeyxxx442": 
			{
				"id": 55555,
				"name": "name3",
				"xvalues": "xxValues",				
				"morevalues": {
					"valuexy": "hello"
				}
			}
		}
	}
}


What I have tried:

Tried jsonToC# and Newtonsoft.Json but creating class from the Response above will cause Errors because the Computers are named as in Response.

public class Computers
{
    public VariableKeyxxx123 variableKeyxxx123 { get; set; }
    public VariableKeyxxx862 variableKeyxxx862 { get; set; }
    public VariableKeyxxx442 variableKeyxxx442 { get; set; }
}
Posted
Updated 18-Apr-18 9:07am
v2
Comments
F-ES Sitecore 18-Apr-18 11:03am    
Rather than deserialising to a concrete class you create yourself you can get Newtonsoft to serialise to a structure of JObject classes that will be able to handle the randomness of structure, but you will need to do some work to traverse the structure the extract the data you want. Google tutorials on newtonsoft and you should find one that involves deserialising to the newtonsoft objects.
Cashi 18-Apr-18 11:18am    
Ok, Thanks. Thought, it would be possible more beautiful.

1 solution

Newtonsoft supports the ExpandoObject, specifically to support JSON that can't be defined by a "schema" and therefore a strongly typed class.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900