问题
I have an app for Windows Phone 7 (written in C# using Visual Studio 2010 + Windows Phone developer tools) that uses data from a web service.
In the past, I have used SOAP which was pretty straightforward (added as a service reference and I was basically ready to go) but now I would like to use JSON instead.
The API offers both a SOAP and a RESTful JSON endpoint and both endpoints provide the same data.
Now my questions:
1) My main motivation to switch from SOAP to JSON is that JSON is supposed to be faster (the response is smaller). Is that correct?
2) How can I use the JSON endpoint in my project? I have tried adding it as a Service Reference but either I missed something or this is not possible.
I am an absolute beginner concerning JSON so help of any kind would be welcome.
回答1:
Beware - Thar be Unicorns!
Whenever you get optimization happy and start investigating solutions it's good to take a deep breath, list a couple of things that describe exactly what you want from both the consumer and server perspective. Be prepared to make concessions where appropriate but don't let yourself deviate too far from your requirements.
Well - What works for you?
Well - I'm glad you asked!
I've been using the ServiceStack ...stack in production for the better part of a year. In fact it's the default provider for my iOS apps because it's just so darn versatile. Basically, you get REST + SOAP all in one implementation, and some very nice optimizations.
https://github.com/mythz/ServiceStack.Text
Plus the API makes JSON really super easy.
var customer = new Customer { Name="Joe Bloggs", Age=31 };
var json = JsonSerializer.SerializeToString(customer);
var fromJson = JsonSerializer.DeserializeFromString<Customer>(json);
You wanna race?
[Insert gratuitous 2 Fast 2 Furious joke here...]
Well yes indeed. I'm a huge fan of Demis Bellot [mythz] and his work, in fact he's working at StackOverflow now. Sufficing to say, he's a perf & optimization nut, so if something is slow, you're almost certain it's not his code that's the issue :-P
Here's some benchmarks to wet your beak:
http://www.servicestack.net/mythz_blog/?p=344
http://daniel.wertheim.se/2011/02/07/json-net-vs-servicestack/
回答2:
JSON can be faster, depending on the server of course. usually JSON results in a smaller response because it doesn't has those xml tags in the response.
to consume a (unprotected) JSON feed you can use the WebClient class:
public void GetData()
{
var client = new WebClient();
client.OpenReadCompleted += new OpenReadCompletedEventHandler(client_GetDataCompleted);
client.OpenReadAsync(new Uri(_address));
}
void client_GetDataCompleted(object sender, OpenReadCompletedEventArgs e)
{
var serializer = new DataContractJsonSerializer(typeof(JSONDataObject),null);
var data = serializer.ReadObject(e.Result) as JSONDataObject;
//do what you need with the data
}
[DataContract(Name = "JSONDataObject")]
public class JSONDataObject
{
[DataMember(Name = "id", Order = 0)]
public int Id
{
get;set;
}
[DataMember(Name = "name", Order = 1, EmitDefaultValue = false)]
public string Name
{
get;set;
}
}
i simplified the code a bit, but i'm using core functionality only so with some research you will be fine. Key classes are WebClient, DataContractJsonSerializer, DataContract, DataMember which are all well documented on MSDN.
来源:https://stackoverflow.com/questions/7311723/use-json-in-wp7-instead-of-soap