RSS Feed for This PostCurrent Article

Use JSON to Transfer Data between .NET and Java Web Services

In my previous post, Java – Use JSON for Data Transfer, I showed you how I passed an array of objects between my Java processes. Here I am going to show you how I used it to pass values between .NET and Java web services.

In the project that I worked on before this, I need to use .NET to invoke a Java web service. There is a situation that I may or may not need to pass extra attributes to the web services that I am going to call. As an example, let’s said the Java service has the following method signature


void method(String arg1, String arg2,……, [array of name value pair])

Depending on the value of the arguments, maybe arg1 or arg2, I need to pass extra attributes in the array of name value pair. In this context, I found JSON is perfectly suitable for what I wanted to achieve.

For JSON in .NET, I used Jayrock. Jayrock is an open source (LGPL) implementation of JSON and JSON-RPC for the Microsoft .NET Framework, including ASP.NET.

As an example, for a list of Person object with name and email, I could pass them as an array of Person objects using JSON.

JsonArray ja = new JsonArray();
ja.Add(new JsonObject(new string[] { "name", "email" },
       new string[] { "jason", "[email protected]"}));
ja.Add(new JsonObject(new string[] { "name", "email" }, 
       new string[] { "paris", "[email protected]" }));

// Print out the JSON string
Console.Out.WriteLine(ja.ToString());

foreach (JsonObject obj in ja)
{
    Console.Out.WriteLine("name: " + obj["name"]);
    Console.Out.WriteLine("email: " + obj["email"]);
}           

The output will be

[{"name":"jason","email":"[email protected]"},
{"name":"paris","email":"[email protected]"}]
name: jason
email: [email protected]
name: paris
email: [email protected]


Trackback URL


Sorry, comments for this entry are closed at this time.