Click here to Skip to main content
15,885,985 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
I have a json file which is having below data.

{
  "ItemDiscount": {
    "enabled": true,
    "enableItemSelectionOnTenderScreen": true,
    "allowDiscountsForCanAdd": true,
    "allowDiscountsForExtra": true,
    "allowDiscountForVMUpcharge": true
  }
}


I am trying to read this file value in my api.

C#
var filedata = File.ReadAllText(jsonFilePath);

var jsonFileData = JsonConvert.DeserializeObject<T>(filedata);


filedata variable value->>

"{\r\n  \"ItemDiscount\": {\r\n    \"enabled\": true,\r\n    \"enableItemSelectionOnTenderScreen\": true,\r\n    \"allowDiscountsForCanAdd\": true,\r\n    \"allowDiscountsForExtra\": true,\r\n    \"allowDiscountForVMUpcharge\": true\r\n  }\r\n}"


What I have tried:

but jsonFileData object shows all values as false false false.. but json file has true value for all proprties
Posted
Updated 18-Oct-22 14:56pm
v2
Comments
PIEBALDconsult 18-Oct-22 14:35pm    
I suppose that some idea of what type T is would help.
Virendra S from Bangalore, Karnataka 19-Oct-22 3:47am    
Issue was with my class model, I have updated now, working fine, thanks!!.

Start by using a converter to generate sample classes from the JSON: Convert JSON to C# Classes Online - Json2CSharp Toolkit[^] is the one I use.

Then compare that to the classes you expect the deserialize to generate. If they aren't very, very similar, then you won't get what you want.

And I suspect that's the problem: you don't normally use a class called "T" in generics as it's generally a placeholder for the generic Type specifier that you explicitly of implicitly use when you run use the generic function / class.
 
Share this answer
 
Comments
Virendra S from Bangalore, Karnataka 19-Oct-22 3:47am    
Issue was with my class model, I have updated now, working fine, thanks!!.
OriginalGriff 19-Oct-22 3:49am    
You're welcome!
JSON Utils: Generate C#, VB.Net, SQL TAble and Java from JSON[^] is my go to for generating C# classes from Json. There are many other tools and helpful information below that will answer many questions for you...

* Working with Newtonsoft.Json in C# & VB[^]
* Working with System.Text.Json in C#[^]

I wrote those articles to answer questions like yours.

Hope it helps!
 
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