Php Serialize Object To Json

  • Jsonencode($array) is a fine way to convert a PHP array or object into a string for saving it into a database. But if we want to save assotiative Arrays. This is what happens: Since JavaScript uses Objects for 'Assotiative arrays', this makes sense.
  • Serializing and Deserializing JSON. The JsonSerializer is able to read and write JSON text directly to a stream via JsonTextReader and JsonTextWriter. Other kinds of JsonWriters can also be used, such as JTokenReader / JTokenWriter, to convert your object to and from LINQ to JSON objects, or BsonReader / BsonWriter, to convert to and from BSON.

Java EE 7 has a JSON API in javax.json, see the Javadoc. From what I can tell, it doesn't have a simple method to marshall any object to JSON, you need to construct a JsonObject or a JsonArray. Import javax.json.

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.3k16 gold badges80 silver badges135 bronze badges
HuiHui
4,8148 gold badges21 silver badges20 bronze badges

15 Answers

You could use the JavaScriptSerializer class (add reference to System.Web.Extensions):

A full example:

T HE ALCHEMIST PICKED UP A BOOK THAT SOMEONE IN THE caravan had brought. Leafing through the pages, he found a story about Narcissus.The alchemist knew the legend of Narcissus, a youth who knelt daily beside a lake to contemplate his own beauty. Download the alchemist book pdf.

Kolappan Nathan
9712 gold badges17 silver badges25 bronze badges
Darin DimitrovDarin Dimitrov
878k233 gold badges3075 silver badges2791 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.6k20 gold badges100 silver badges120 bronze badges
mschmoockmschmoock
13.4k5 gold badges27 silver badges31 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 JavaScriptSerializer. But personally I still prefer Json.NET.

EdgarEdgar
3,0164 gold badges35 silver badges54 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,4678 gold badges59 silver badges104 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

If you are in an ASP.NET MVC web controller it's as simple as:

Jul 10, 2018  Allok Video Converter is the best vide converter to convert the video to different formats to play on multiple devices at anytime and anywhere. The free video converter has powerful engine to convert the videos with high quality and fast speed. Allok Video to MP4 Converter is a easiest-to-use video converter software for portable MP4 Player, iPod, Apple TV, PSP, PS3, Zune, Xbox 360, ARCHOS, Plam etc. Allok 3gp video converter free. download full version. Allok Video Converter is an All-in-One video converting tool which can convert all video files to AVI, MPEG, WMV, MP4, H264, 3GP, FLV etc. Allok Video Converter includes a powerful media conversion engine internally so that you can convert media files with very fast speed and excellent video quality.

Can't believe no one has mentioned this.

micahhoovermicahhoover
1,3595 gold badges26 silver badges47 bronze badges
Peter Mortensen
14.5k19 gold badges89 silver badges118 bronze badges
HitheshHithesh

As easy as this, works for dynamic objects as well (type object):

Chandan Kumar
3,2503 gold badges29 silver badges55 bronze badges
MarzSocksMarzSocks
2,9702 gold badges16 silver badges29 bronze badges

Use this tools for generate C# class, then use this code to serialize your object

Php Serialize Object To Json Software

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/

Serialize
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

Php Serialize Object

C#

VBNET EXAMPLE

End Namespace

The second step is to create a simple test data like this:

C#

VB.NET

And this is the final result:

802. 11n usb wireless drivers download. Remember to add a reference to system.web.extension.dll in order to achive your goal.

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.