0

I'm having difficulty after I Deserialize the JSON object I am getting from an API using JsonPropertyAttribute

I successfully get the value from the JSON object

{
    "property name": "property value"
}

but my problem is, instead of the class property display like so:

{
    "propertyName": "property value"
}

I only see the property names from JSON that I was fetching:

{
    "property name": "property value"
}

Is there an easy way to make it display the property name of the class object instead of the ones from JSON that I fetched?

EDIT:

Here is how the class looks:

public class JsonObjectDto {
    [JsonProperty(PropertyName = "property name")]
    public string propertyName { get; set; }
}

And I do something similar to this example:

public class HomeController : Controller {
    public ActionResult Index() {
        string jsonResponseString = "{\"property name\":\"property value\"}";
        JsonObjectDto result = JsonConvert.Deserialize<JsonObjectDto>(jsonResponseString);
        
        if(result != null) {
            return Ok(result);
        }
        throw new Exception("fetch response error");
    }
}

I am hoping to achieve to the response is to make it like this output:

{
    "propertyName": "property value"
}
aj go
  • 637
  • 2
  • 10
  • 27
  • 1
    Where are you looking to see the property names? The JSON stays the same. The class remains the same. Are you serializing the class again and seeing the original property names? That makes sense if you've marked them `[JsonProperty("property name")]` since that tells the serializer to use that name in JSON, both ways. – Heretic Monkey Apr 30 '21 at 13:06
  • can you copy the structure of your class – Krishna Varma Apr 30 '21 at 13:25
  • 1
    Is the first space in the second code block `" property value"` a typo? – Self Apr 30 '21 at 13:41
  • 1
    Related : https://stackoverflow.com/questions/44632448/use-different-name-for-serializing-and-deserializing-with-json-net – Self Apr 30 '21 at 13:43
  • I tried the answer written in that link but what happened to my side was, it didn't fetched the value of the json property at all. – aj go Apr 30 '21 at 14:26
  • As mentioned by @Self you can use a custom contract resolver to map your c# property to different JSON property names when deserializing vs re-serializing. See [Json.NET deserialize or serialize json string and map properties to different property names defined at runtime](https://stackoverflow.com/a/38112291/3744182) and [Use different name for serializing and deserializing with Json.Net](https://stackoverflow.com/q/44632448/3744182). Do those two questions adequately answer yours also, or do you need more specific help? – dbc Apr 30 '21 at 14:31

1 Answers1

1

Simply Remove JsonProperty from class and Before Deserializing Object Replace "property name" with "propertyname"

    public ActionResult Index() 
    {
        string jsonResponseString = "{\"property name\":\"property value\"}";
        jsonResponseString=jsonResponseString.Replace("property name","propertyName");
        JsonObjectDto result = JsonConvert.Deserialize<JsonObjectDto>(jsonResponseString);
                if(result != null) {
            return Ok(result);
        }
        throw new Exception("fetch response error");
    }
Ashfaque
  • 11
  • 3