I need to serialize some objects to a JSON and send to a WebService. How can I do it using the org.json library? Or I'll have to use another one? Here is the class I need to serialize: public class. Serialize method in JsonConvert class converts the C# object into a JSON string. Pass the object as a parameter in Serialize method. Create a solution and add a.
Active1 month ago
I have classes like these:
And I would like to turn a
Lad object into a JSON string like this:
(without the formatting). I found this link, but it uses a namespace that's not in .NET 4. I also heard about JSON.NET, but their site seems to be down at the moment, and I'm not keen on using external DLL files. Are there other options besides manually creating a JSON string writer?
Liam
17.3k1616 gold badges8080 silver badges135135 bronze badges
HuiHui
4,81488 gold badges2121 silver badges2020 bronze badges
15 Answers
You could use the
JavaScriptSerializer class (add reference to System.Web.Extensions ):
A full example:
Kolappan Nathan
97122 gold badges1717 silver badges2525 bronze badges
Darin DimitrovDarin Dimitrov
878k233233 gold badges30753075 silver badges27912791 bronze badges
Since we all love one liners
.. this one depends on the Newtonsoft NuGet package, which is popular and better than the default serializer.
Documentation: Serializing and Deserializing JSON
James Newton-King
32.6k2020 gold badges100100 silver badges120120 bronze badges
mschmoockmschmoock
13.4k55 gold badges2727 silver badges3131 bronze badges
Use Json.Net library, you can download it from Nuget Packet Manager.
Serializing to Json String:
Deserializing to Object:
Gokulan P HGokulan P H
Use the
DataContractJsonSerializer class: MSDN1, MSDN2.
My example: HERE.
It can also safely deserialize objects from a JSON string, unlike Edgar
JavaScriptSerializer . But personally I still prefer Json.NET.
C# Deserialize JsonEdgar
3,01644 gold badges3535 silver badges5454 bronze badges
Wooou! Really better using a JSON framework :)
Here is my example using Json.NET (http://james.newtonking.com/json):
The test:
The result:
Now I will implement the constructor method that will receives a JSON string and populates the class' fields.
Majid
8,46788 gold badges5959 silver badges104104 bronze badges
Jean J. MichelJean J. Michel
You can achieve this by using Newtonsoft.json. Install Newtonsoft.json from Nuget. and then:
Waleed NaveedWaleed Naveed
If they are not very big, whats probably your case export it as Json.Also this makes portable among all plattforms
user8426627user8426627
C# Serialize Object To Json String
If you are in an ASP.NET MVC web controller it's as simple as:
C Sharp Serialize Object To Json
Can't believe no one has mentioned this.
micahhoovermicahhoover
1,35955 gold badges2626 silver badges4747 bronze badges
Peter Mortensen
14.5k1919 gold badges8989 silver badges118118 bronze badges
HitheshHithesh
Dell widcomm bluetooth software. As easy as this, works for dynamic objects as well (type object):
Chandan Kumar
3,25033 gold badges2929 silver badges5555 bronze badges
MarzSocksMarzSocks
2,97022 gold badges1616 silver badges2929 bronze badges
Use this tools for generate C# class, then use this code to serialize your object
Artem PolishchukArtem Polishchuk
I would vote for ServiceStack's JSON Serializer:
It is also the fastest JSON serializer available for .NET:http://www.servicestack.net/benchmarks/
JamesJames
wchowardwchoward
There is this really nifty utility right here: http://csharp2json.io/
jallenjallen
Take care to create your class with the right attribute too:
Create this class with <Serializable> attribute as per the example C# example followed by vb.net exmpale
C#
VBNET EXAMPLE
End Namespace
The second step is to create a simple test data like this:
Java games download. C#
VB.NET
And this is the final result:
Remember to add a reference to system.web.extension.dll in order to achive your goal. https://yurllv.weebly.com/blog/macbook-pro-serial-number-format.
makemoney2010makemoney2010
protected by Brian RogersNov 23 '14 at 0:39
Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead? Not the answer you're looking for? Browse other questions tagged c#.netjsonserialization or ask your own question.
Active11 months ago
I am trying to serialize a C# object into a Json object. That will then be submitted to the Salesforce API, and create an application. Right now I have the C# object serialized into a Json string, but I need it to be an object.
Here is my C# object along with accompany serialization.
I need the JSON string to output inside of a JSON Object. An example of what I need is below:
This hard coded json string is inside of an object. As it stands, the values in the C# object are being outputted into a JSON string, but I'm needing it output into an object so that the Salesforce API will accept the submission.
How can I append or insert the JSON string into an object?
Aleksandr Ivanov
2,10833 gold badges2121 silver badges3535 bronze badges
MowriteMowrite
4 Answers
To create correct JSON first you need to prepare appropriate model. It can be something like that:
C# Json Serializer
To be able to use
Data attributes you will need to choose some other JSON serializer. For example DataContractJsonSerializer or Json.NET(I will use it in this example).
So Aleksandr IvanovAleksandr Ivanov
jsonCreditApplication variable will be:
2,10833 gold badges2121 silver badges3535 bronze badges
Another way.
and
see
My packages.config at the time of writing..though I'm sure future/latest versions will still support it:
granadaCodergranadaCoder
16.3k55 gold badges5656 silver badges8787 bronze badges
Install Newtonsoft.Json NuGet then decorate Customer class with the requires naming decorations to tell Json serializer how to serialize the customer class fields:
Next, serialize the object like this:
You will get the desired result and the value of
JsonOutput will be :'{'jsonCreditApplication':{'gors_descr':'Appliances ','b_name_first':'Marisol ','b_name_last':'Testcase '}}'
There are many ways to do this but I believe that this one is the simplest solution.
ZuhairZuhair
32911 gold badge33 silver badges1212 bronze badges
You could use something like http://restsharp.org/, which is a c# library for REST. If so, it has a built in serializer for json objects (.addJsonBody()) or you can serialize it yourself and add with
Alternatively if you want more control over it you can use
I've also found https://github.com/ademargomes/JsonRequest, but it's still in development.Be warned that if you use something like RestSharp, it is a canned request so any variation from what they have created as the standard requests (e.g. multipart/form data w/ json or custom headers or even custom authentication) may not work with their library, in which case it's probably better to make your own using HttpWebRequest anyway. Hope that helps!
Benj ArnoppBenj Arnopp
Not the answer you're looking for? Browse other questions tagged c#jsonapiserialization or ask your own question.Comments are closed.
|
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |