Click here to Skip to main content
14,326,463 members
   

C#

 
GeneralRe: 'Switch on 'Type: C# 7,8 offer no relief; and, Eric Lippert is still right Pin
honey the codewitch20-Jul-19 10:47
memberhoney the codewitch20-Jul-19 10:47 
GeneralRe: 'Switch on 'Type: C# 7,8 offer no relief; and, Eric Lippert is still right Pin
BillWoodruff20-Jul-19 13:34
mveBillWoodruff20-Jul-19 13:34 
GeneralRe: 'Switch on 'Type: C# 7,8 offer no relief; and, Eric Lippert is still right Pin
honey the codewitch20-Jul-19 13:38
memberhoney the codewitch20-Jul-19 13:38 
QuestionProblem deserialising JSON data [SOLVED by the brilliant Richard Deeming] Pin
Richard MacCutchan18-Jul-19 2:03
protectorRichard MacCutchan18-Jul-19 2:03 
AnswerRe: Problem deserialising JSON data Pin
OriginalGriff18-Jul-19 2:35
protectorOriginalGriff18-Jul-19 2:35 
GeneralRe: Problem deserialising JSON data Pin
Richard MacCutchan18-Jul-19 3:06
protectorRichard MacCutchan18-Jul-19 3:06 
GeneralRe: Problem deserialising JSON data Pin
Richard MacCutchan18-Jul-19 4:05
protectorRichard MacCutchan18-Jul-19 4:05 
AnswerRe: Problem deserialising JSON data Pin
Richard Deeming18-Jul-19 4:42
mveRichard Deeming18-Jul-19 4:42 
The DataContractJsonSerializer is not particularly flexible, but you can make this work. (Assuming you're using .NET 4.5 or later.)

.net - Any way to make DataContractJsonSerializer serialize Dictionaries properly? - Stack Overflow[^]

Based on your sample JSON, the classes I came up with are:
public class PropertyValue
{
    public int id { get; set; }
    public string org_name { get; set; }
    public string value { get; set; }
}

public class Role
{
    public int id { get; set; }
    public string name { get; set; }
    public string suffix { get; set; }
}

public class Volunteer
{
    public int id { get; set; }
    public IList<Role> roles { get; set; }
    public IList<IDictionary<string, PropertyValue>> volunteer_properties { get; set; }
}

public class Root
{
    public Volunteer volunteer { get; set; }
}
With those classes in place:
var ms = new MemoryStream(Encoding.UTF8.GetBytes(json));
var ser = new DataContractJsonSerializer(typeof(Root), new DataContractJsonSerializerSettings(){ UseSimpleDictionaryFormat = true });
var root = (Root)ser.ReadObject(ms);

The volunteer_properties property is a pain - it's a list of dictionaries, each containing a single entry using the property name as a key. If you switched to JSON.NET, you could use a custom converter to tidy that up. But if you can change the source JSON, you could simplify it to:
"volunteer_properties": {
    "first_name":{
        "id":3990,
        "org_name":"First Name",
        "value":"Richard"
    },
    "surname":{
        "id":3991,
        "org_name":"Surname",
        "value":"MacCutchan "
    },
    //
    // a number of other entries in the same form
    //
}
which would then let you remove the IList<...> from the property declaration:
public IDictionary<string, PropertyValue> volunteer_properties { get; set; }
giving you a single dictionary of properties.



"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer

GeneralRe: Problem deserialising JSON data Pin
Richard MacCutchan18-Jul-19 6:02
protectorRichard MacCutchan18-Jul-19 6:02 
QuestionGetting Exception Window Class Name is not Valid in Windows Application using vb.net Pin
Member 1453381917-Jul-19 11:29
memberMember 1453381917-Jul-19 11:29 
AnswerRe: Getting Exception Window Class Name is not Valid in Windows Application using vb.net Pin
Luc Pattyn17-Jul-19 11:49
professionalLuc Pattyn17-Jul-19 11:49 
GeneralRe: Getting Exception Window Class Name is not Valid in Windows Application using vb.net Pin
Member 1453381917-Jul-19 12:00
memberMember 1453381917-Jul-19 12:00 
GeneralRe: Getting Exception Window Class Name is not Valid in Windows Application using vb.net Pin
Luc Pattyn17-Jul-19 12:13
professionalLuc Pattyn17-Jul-19 12:13 
AnswerRe: Getting Exception Window Class Name is not Valid in Windows Application using vb.net Pin
Eddy Vluggen17-Jul-19 13:51
mveEddy Vluggen17-Jul-19 13:51 
AnswerRe: Getting Exception Window Class Name is not Valid in Windows Application using vb.net Pin
phil.o17-Jul-19 22:13
mvephil.o17-Jul-19 22:13 
AnswerRe: Getting Exception Window Class Name is not Valid in Windows Application using vb.net Pin
Richard Deeming17-Jul-19 22:52
mveRichard Deeming17-Jul-19 22:52 
QuestionOutOfMemoryException help please Pin
DTGeek17-Jul-19 5:07
memberDTGeek17-Jul-19 5:07 
AnswerRe: OutOfMemoryException help please Pin
OriginalGriff17-Jul-19 5:21
protectorOriginalGriff17-Jul-19 5:21 
AnswerRe: OutOfMemoryException help please Pin
Gerry Schmitz17-Jul-19 6:07
mveGerry Schmitz17-Jul-19 6:07 
GeneralRe: OutOfMemoryException help please Pin
DTGeek17-Jul-19 6:11
memberDTGeek17-Jul-19 6:11 
AnswerRe: OutOfMemoryException help please Pin
Luc Pattyn17-Jul-19 7:42
professionalLuc Pattyn17-Jul-19 7:42 
GeneralRe: OutOfMemoryException help please Pin
DTGeek17-Jul-19 8:17
memberDTGeek17-Jul-19 8:17 
GeneralRe: OutOfMemoryException help please Pin
Luc Pattyn17-Jul-19 8:20
professionalLuc Pattyn17-Jul-19 8:20 
GeneralRe: OutOfMemoryException help please Pin
Luc Pattyn17-Jul-19 12:53
professionalLuc Pattyn17-Jul-19 12:53 
AnswerRe: OutOfMemoryException help please Pin
Eddy Vluggen17-Jul-19 13:50
mveEddy Vluggen17-Jul-19 13:50 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.