tags:

views:

69

answers:

4

Is there any tool out there can convert Java's JSON to/from C#?

+5  A: 

JSON is a data interchange format (JavaScript Object Notation) that is not tied to either Java or .NET or any specific implementation.

Are you looking for a JSON library for .NET? There are links to many libraries for different platforms and languages at the bottom of the linked page.

Oded
I have mislead everyone again! What i was asking is I want to know is there any mapping tool out there can convert "Java generated json object" into C# object, and vice vance.
Bryan Fok
@Bryan Fok - perhaps you should read [this](http://benalman.com/news/2010/03/theres-no-such-thing-as-a-json/). JSON is data notation - you seem to want a .NET library that can read JSON and generate C# classes from it (or deserialize the JSON into existing C#). Can you please edit your question and clarify _exactly_ what you are looking for?
Oded
+1  A: 

AFAIK Json is not server-side-language-dependent, since it applies to JavaScript, not strictly to Java or C#. Given this, a Json correctly formatted should correctly work with any parser in any server-side language. If you are searching a C# implementation for Json handling, take a look at these sources:

NinjaCross
A: 

And to complement with Java-side tools (which are aplenty as well), I suggest having a look at Jackson JSON parser/databinder. For other suggestions there are many many SO questions, such as this one.

StaxMan
I have mislead everyone again! What i was asking is I want to know is there any mapping tool out there can convert "Java generated json object" into C# object, and vice vance.
Bryan Fok
Ah, then ignore my suggestion. :-)Btw: you can still edit the question to maybe clarify wording a bit?
StaxMan
A: 

In .NET you can use the DataContract and DataMember attributes from the System.Runtime.Serialization to define the variables of the Json object like this

using System.Runtime.Serialization;

[DataContract]
class MyJSONObject
{
    [DataMember(Name="property1")]
    public int Property1
    {
        get;
        set;
    }

    [DataMember(Name = "property2")]
    public int Property2
    {
        get;
        set;
    }
}   

Then, provided you are using .NET 3.5 or later, you can use the DataContractJsonSerializer class from the System.Runtime.Serialization.Json namespace to serialize your object to a JSON string or deserialize a JSON string to your object.

Adam Burk
I have mislead everyone again! What i was asking is I want to know is there any mapping tool out there can convert "Java generated json object" into C# object, and vice vance.
Bryan Fok
As mentioned by Oded, there is no difference between JSON generated by Java and any other JSON. It is notation used to represent an object. If you need to convert JSON to/from a C# object you should use the DataContractJsonSerializer. If you need to do the conversion on the Java side I would recommend looking at Jackson mentioned by StaxMan
Adam Burk