我有一个WCF Service POST方法,如下所示:
[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedResponse, ResponseFormat = WebMessageFormat.Json,
RequestFormat = WebMessageFormat.Json, UriTemplate = "GetResult/{schoolid}")]
[return: MessageParameter(Name = "ExamResult")]
CResults GetExamResult(CExamResultQuery pObj, string schoolid);
这是CResults Class:
public class CResults
{
public string studentname { get; set; }
public string studentid { get; set; }
public string telugu { get; set; }
public string hindi { get; set; }
public string english { get; set; }
public string mathematics { get; set; }
public string science { get; set; }
public string social { get; set; }
public string totalresult { get; set; }
}
我在客户端的Post响应中收到了一个JSON字符串,但Key(Value)的顺序不同。这是JSON:
"{\"ExamResult\":{\"english\":\"80\",\"hindi\":\"80\",\"mathematics\":\"80\",\"science\":\"80\",\"social\":\"80\",\"studentid\":\"50\",\"studentname\":\"Ashok\",\"telugu\":\"80\",\"totalresult\":\"PASS\"}}"
我看到JSON收到的字母顺序不像Class那样 变量顺序。如何以与我班级相同的方式制作JSON订单。
以下是我如何提取Keys&来自JSON字符串的值
JSONObject jsonRootObject = new JSONObject(jsonData);
JSONObject innerObj = jsonRootObject.getJSONObject("ExamResult");
Iterator<?> keys = innerObj.keys();
while (keys.hasNext()) {
String key = (String) keys.next();
mActivity.updateResultsTable(""+key+": "+innerObj.get(key).toString());
// Log.i("testExtrace JSONObject", "key " + key + ": value " + innerObj.get(key).toString());
}
我知道我可以使用如下的键提取JSON值:
String studentName= jsonRootObject.getString("studentname");
我只想更改WCF类,这可能有助于按照我期望的顺序提取任何JSON。
感谢您的支持
答案 0 :(得分:1)
您可以使用如下所示的DataMember(订单)属性:
[DataContract]
public class CResults
{
[DataMember(Order = 0)]
public string studentname { get; set; }
[DataMember(Order = 1)]
public string studentid { get; set; }
[DataMember(Order = 2)]
public string telugu { get; set; }
[DataMember(Order = 3)]
public string hindi { get; set; }
[DataMember(Order = 4)]
public string english { get; set; }
[DataMember(Order = 5)]
public string mathematics { get; set; }
[DataMember(Order = 6)]
public string science { get; set; }
[DataMember(Order = 7)]
public string social { get; set; }
[DataMember(Order = 8)]
public string totalresult { get; set; }
}