0

I have a class like so:

public class ResearchTerm
{
    [JsonProperty("1")]
    public string Term { get; set; }
    [JsonProperty("2")]
    public int Count { get; set; }
    [JsonProperty("3")]
    public long Score { get; set; }
}

A JSON array in a string:

[{"1":"ifn","2":7,"3":1.81818181818182},{"1":"macrophages","2":5,"3":1.2987012987013},{"1":"n =","2":5,"3":1.2987012987013},{"1":"p <","2":5,"3":1.2987012987013}]

And this line of code that converts the JSON array to an array of type ResearchTerm:

var researchTerms = Newtonsoft.Json.JsonConvert.DeserializeObject<ResearchTerm[]>(personTask.Result.ResearchKeywords);

This is working beautiful and return an array of ResearchTerms with the property names in my class. The problem is that when I serialize this for my API to return, it's converting it all back to property names 1, 2, and 3. How can I use JsonProperty to deserialize like it's currently doing, but not serialize? I don't really care about this data going back in, so I don't need to serialize it once it's retrieved from the database as this string and deserialized into my ResearchTerm class.

Andy
  • 1,243
  • 3
  • 22
  • 40
  • Note that I don't want to ignore the fields on serialization, but to ignore renaming the properties back from the class names to the Json property names. I'd like to keep my ResearchTerms with the non-numerical property names upon serialization. – Andy Sep 22 '17 at 01:20
  • Looks like a duplicate of [How to ignore JsonProperty(PropertyName = “someName”) when serializing json?](https://stackoverflow.com/q/20622492). – dbc Sep 22 '17 at 01:29
  • I don't think this is a duplicate exactly as I'm using AutoMapper and not calling anything to explicitly serialize the object. – Andy Sep 22 '17 at 01:42
  • Andy - AutoMapper wasn't mentioned in the question, so I didn't know that. Can you show [edit] the question and add a [mcve] showing what you're currently doing? If you're calling some higher-level API and don't have direct access to the contract resolver then that solution definitely isn't sufficient - but we can only help if we know what that top-level API is. – dbc Sep 22 '17 at 03:17

0 Answers0