I have a web service that receives a JSON string as a parameter. I have only been able to successfully send this when my web method's parameter is a generic type 'object'.
Can I serialize this generic object to a string or a custom object? Do I need to modify the parameter type of this method? Any help would be awesome.
Here is the web method:
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string StoreDataOut(object json)
{
//serialization magic
}
This is the test JSON that is b开发者_如何学编程eing passed to this web method:
{
"uid":"1234abcd",
"application":"Application Name",
"eventName":null,
"clienttoken":"Test Client",
"version":"1.0.0",
"datetime":"1/1/2011 12:00 AM",
"data":[
{
"id":"alpha_123",
"question":"ronunciations in pre-classical times or in non-Attic dialects. For det",
"answer":"nunciations "
},
{
"id":"beta_456",
"question":"official documents such as laws an",
"answer":"or modif"
},
{
"id":"gamma_789",
"question":" maintained or modified slightly to fit Greek phonology; thus, ?",
"answer":"iation of Attic"
},
{
"id":"delta_098",
"question":"econstructed pronunciation of Attic in the late 5th an",
"answer":"unciation of "
},
{
"id":"epsilon_076",
"question":"erent stylistic variants, with the descending tail either going straight down o",
"answer":"Whole bunch"
},
{
"id":"zeta_054",
"question":"rough breathing when it begins a word. Another diacritic use",
"answer":"other dia"
}
]
}
You need a class as follows and use that as the type in your webmethod instead of object.
class JsonDTO
{
public JsonDTO()
{
data = new List<data>();
}
public string uid {get; set;}
public string application {get;set}
public string eventName {get; set;}
public string clienttoken {get;set}
public string version {get;set;}
public string @datetime {get; set;}
public List<data> data {get; set;}
}
public class data
{
public string id {get; set;}
public string question {get; set;}
public string answer {get; set;}
}
You should be able to have the .Net framework correctly serialise most objects so your web method signature looks like this:
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string StoreDataOut(MyClass input);
But I've found that certain objects it has probelms with, and so my fallback method is to accept a string instead (which will be the serialised JSON), and deserialise it myself either using the JavaScriptSerializer class or a JSON serialisation library like Json.Net.
This is an example of deserialising an object using the JavaScriptSerializer
class where I separate the "actual" method out with a wrapper method that handles the deserialistion for us:
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string StoreDataOut(string input)
{
var serialiser = new JavaScriptSerializer();
MyClass deserialisedInput = serialiser.Deserialize<MyClass>(input);
return (StoreDataOutImpl deserialisedInput);
}
private string StoreDataOutImpl(MyClass input);
This gives you the flexibility of being able to control the serialisation using JavaScriptConverters or using a completely different library (e.g. Json.Net).
This will require that you have a class MyClass
that is correctly formatted to recieve the input JSON. If you don't then you can can just get the serialiser to output a dictionary which will contain key - value pairs corresponding to the properties of the serialised JSON object:
var deserialisedInput = (Dictionary<string, object>)serialiser.DeserializeObject(input);
精彩评论