2

I'm getting a bunch of JSON data back from a 3rd party API like this:

returnValue = data["value"].ToObject<List<T>>();

All but one of the fields are just basic name:value pairs like this:

"Name":"Value"

I map the values I need to a class like this:

public sealed class Project
{
    public string Id { get; set; }
    public string Title { get; set; }
    public string Url { get; set; }
    public DateTime ProjectDateLocal { get; set; }
    public string ParentFolderName { get; set; }
    public string ParentFolderId { get; set; }
    //causing trouble
    public Int32 ProjectTypeId { get; set; }

    public string PlayerUrl
    {
        get
        {
            return "http://em.edu.edu/prj/Play/" + this.Id;
        }
    }
}

However, one name:value pair is complicated like this:

"CustomFieldValues":[
        {
          "FieldName":"ProjectTypeId","FieldDefinitionId":"37a2ffeb3bd441f6a60158458910a04d40","DataType":"Integer","Value":"100105"
        }
      ]

I only need the FieldName(ProjectTypeId) and Value, is there a way to get just that have the class recognize that and set it in my ProjectTypeId property?

Thanks!

0

2 Answers 2

3

As @viggity stated, you can use Newtonsoft for your problem and the solution provided is good. The only thing you have to do is provide a good string json to the Deserializer.

If you want a simpler solution why don't you use data["value"].ToObject<List<Project>>() ? Note: Assigning attributes like [JsonProperty("FieldNameFromJson")] is ussefull for mappings.

See this post for more info about how you can do this.

Sign up to request clarification or add additional context in comments.

Comments

2

Use Json.net to deserialize JsonConvert.Deserialize<Project>(jsonStringContent)

Json.net will go multi levels, just add a new class and have your Project have that property.

public class CustomFieldValue
{
    public string FieldName {get;set;}
    public string Value {get; set;}
}

and add a list of them to your Project.

public sealed class Project
{
    public string Id { get; set; }
    ...
    public List<CustomFieldValue> CustomFieldValues { get; set; }
}

Json.net won't have any problem with it. If you don't add FieldDefinitionId, etc then Json.net will just ignore it.

http://www.newtonsoft.com/json

6 Comments

Thanks! Do you mean use: JsonConvert.Deserialize<Project>(jsonStringContent) , instead of: returnValue = data["value"].ToObject<List<T>>();
In the above, <T> = <Project>
I'm not sure where your stringified json is in. My guess is it is in data["value"], right? if so then returnValue = JsonConvert.Deserialize<Project>(data["value"])
Did you mean to use 'JsonConvert.DeserializeObject'? I couldn't find JsonConvert.Deserialize. Thanks.
This won't work because 'returnValue' needs to be type System.Collections.Generic.List<T>
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.