JSON field's names in the Web Service response [duplicate] - c#

This question already has answers here:
Serializing F# Record type to JSON includes '#' character after each property
(2 answers)
Closed 7 years ago.
I use F#, but I believe the question is not F# specific.
I have the interface for Web Service:
[<ServiceContract>]
type IRestService =
[<OperationContract>]
[<WebGet(UriTemplate = "Maintenance", ResponseFormat=WebMessageFormat.Json)>]
abstract GetMaintenancesRest: a:unit -> Maintenance[]
When I am trying to use this service I can get JSON, but all field's names in the JSON have the symbol '#':
[{"Address#":"one","Assetid#":"","Assignmentdate#":"/Date(1434147917730-0700)/","Comment#":"" ...
Why and how can I fix it?

It's F#. I ran into this a long time ago, so I might be forgetting. As I recall you may not use records to return from the service, or you get this error. You need class types with properties. I don't think mutable records work either. I also don't think public fields work; it's got to be public read/write properties.

Related

Is there any way to get my json without creating a class [duplicate]

This question already has answers here:
How do you read a simple value out of some json using System.Text.Json?
(8 answers)
Closed 11 months ago.
From an API call I am getting some JSON data, I dont want to declare a class, while in NewtonSoft we can get those data as given below without creating a class,
JObject o1 = JObject.Parse(File.ReadAllText(#"example.json"));
Console.WriteLine(o1["customername"]);
Is there any similar way to get json data without creating a class in System.Text.Json
Edit:
I was able to proceed by the solutions provided here
How do you read a simple value out of some json using System.Text.Json?
You could try using JSON document model and parse subsections you are interested in.
See this link https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json-use-dom-utf8jsonreader-utf8jsonwriter?pivots=dotnet-6-0#deserialize-subsections-of-a-json-payload

Json with c# form [duplicate]

This question already has answers here:
How can I deserialize JSON with C#?
(19 answers)
Closed 5 years ago.
i have this json with data
http://84.235.49.85:58/saws/hi/is/inserSer?jsonObject={
"authObject":{
"userName":"7070",
"password":"123",
"baladyCode":"07",
"amanaCode":"061"
},
"billObj":{
"billNumber":"0611138000302",
"billCreationDate":"07-01-1438",
"billDetails":[
{"depId":"11006","billValue":"1"}
]}
}
authObject Obj:-
this object holds authentication data about user that will be used to decide can we allow accessing for this request or not.
billObj Obj:-
this object holds bill data that will be used to create insertion operation, let us say that every bill may have one sub-account or many sub-accounts so we make it dynamic and give you to create Json Array of bill's details that holds every sub-account and its value
any one help me for creating this code in c# form . i need full code for this example please .thanks
You must use a library for that.
I recommend you Newtonsoft.JSON
It's easy to use, and you can download it via nuget.
You must create a class and just fun!
ej: https://pastebin.com/6sVd1TF0

How to know if json can convert to a given type [duplicate]

This question already has answers here:
Deserialize json in a "TryParse" way
(7 answers)
Closed 6 years ago.
The json token.ToObject<T> method can throw if json is not able to convert the data in token the to the type T.
I would like to do a early test and need a method like this:
bool JsonConvert.CanConvert<TSrc, TDest>();
I don't find any reference to something close to it.
[Precision]
Actually TSrc here is not composed but a basic type.
[Edit]
When I say that I want to do an early test, I talk about a test done before any attempt to parse somethings.
Actually I don't have any json file around when I need to do the test.
So the TryParse or the TryCatch pattern doesn't match my need.
Why an early test?
Just enclose the call with a try-catch. What you're suggesting would internally parse the string as well, so speed-wise it would make no difference. Better yet, it will be parsed only once. If it succeeds, fine. If not, you'll know too.

What does the [] represent in C# [duplicate]

This question already has answers here:
What are attributes in .NET?
(11 answers)
Closed 9 years ago.
i've been developing in c# for 4 month and I still dont know what does the [] means in entity framework.
Here an example
[Column("mycolumn")]
public int Column {get;set;}
What is it functionality?
Its there another situation that i have to use it or just with entity framework?
Square brackets [ & ] mean a few different things in C#, but in this case they are saying that "Column" is an Attribute. An attribute is basically design time information that you add to classes or properties for various reasons. You can also make your own ;)

How do I assign .Net attributes programmatically? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Can attributes be added dynamically in C#?
Is it possible to assign .net Attribute to class/method programmatically?
For example:
Can I decorate my custom .net com classes with Guid/ProgId attributes taken from external file? Something like:
typeof(MyComObject).AssignAttribute(new GuidAttribute("..."));
instead of hardcode like:
[Guid("...")]
class MyComObject
{
}
Thank you in advance!
It depends. ICustomTypeDescriptor allows to almost anything you want to almost every part of a class (which might not even exist for that matter), but this particular interface might not be used by whatever system you're trying to feed your object to. PropertyGrid uses this interface extensively, though.

Categories