我的问题是:json反序列话之后赋值给实体的时候 public string errMsg; public string errNum; 这两个能赋值上但是 public Dictionary<string, string> retData;这个就不行,不知道为什么,下面是一些代码:
查询后返回的json:
{
"errNum": 0,
"errMsg": "success",
"retData": {
"from": "en",
"to": "zh",
"dict_result": {
"word_name": "hello",
"symbols": [
{
"ph_am": "h??lo?",
"ph_en": "h?'l??",
"parts": [
{
"part": "int.",
"means": [
"哈喽,喂",
"你好,您好",
"表示问候",
"打招呼"
]
},
{
"part": "n.",
"means": [
"“喂”的招呼声或问候声"
]
},
{
"part": "vi.",
"means": [
"喊“喂”"
]
}
]
}
]
}
}
}
这是是实体
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace WindowsFormsApplication1
{
[Serializable]
public class Word
{
public string errMsg;
public string errNum;
public Dictionary<string, string> retData;
}
}
这是json转换类
/// <summary>
/// Json序列化和反序列化辅助类
/// </summary>
using System.Runtime.Serialization.Json;
using System.IO;
using System.Text;
using System.Data;
using System.Web.Script.Serialization;
using System.Collections.Generic;
using System;
public class JsonHelper
{
/// <summary>
/// Json序列化
/// </summary>
public static string JsonSerializer<T>(T obj)
{
string jsonString = string.Empty;
try
{
DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(T));
using (MemoryStream ms = new MemoryStream())
{
serializer.WriteObject(ms, obj);
jsonString = Encoding.UTF8.GetString(ms.ToArray());
}
}
catch
{
jsonString = string.Empty;
}
return jsonString;
}
/// <summary>
/// Json反序列化
/// </summary>
public static T JsonDeserialize<T>(string jsonString)
{
T obj = Activator.CreateInstance<T>();
try
{
using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonString)))
{
DataContractJsonSerializer ser = new DataContractJsonSerializer(obj.GetType());//typeof(T)
T jsonObject = (T)ser.ReadObject(ms);
ms.Close();
return jsonObject;
}
}
catch
{
return default(T);
}
}
// 将 DataTable 序列化成 json 字符串
public static string DataTableToJson(DataTable dt)
{
if (dt == null || dt.Rows.Count == 0)
{
return "\"\"";
}
JavaScriptSerializer myJson = new JavaScriptSerializer();
List<Dictionary<string, object>> list = new List<Dictionary<string, object>>();
foreach (DataRow dr in dt.Rows)
{
Dictionary<string, object> result = new Dictionary<string, object>();
foreach (DataColumn dc in dt.Columns)
{
result.Add(dc.ColumnName, dr[dc].ToString());
}
list.Add(result);
}
return myJson.Serialize(list);
}
// 将对象序列化成 json 字符串
public static string ObjectToJson(object obj)
{
if (obj == null)
{
return string.Empty;
}
JavaScriptSerializer myJson = new JavaScriptSerializer();
return myJson.Serialize(obj);
}
// 将 json 字符串反序列化成对象
public static object JsonToObject(string json)
{
if (string.IsNullOrEmpty(json))
{
return null;
}
JavaScriptSerializer myJson = new JavaScriptSerializer();
return myJson.DeserializeObject(json);
}
// 将 json 字符串反序列化成对象
public static T JsonToObject<T>(string json)
{
if (string.IsNullOrEmpty(json))
{
return default(T);
}
JavaScriptSerializer myJson = new JavaScriptSerializer();
return myJson.Deserialize<T>(json);
}
}
点击窗体按钮时执行:
private void button1_Click(object sender, EventArgs e)
{
string result = request(url, param);
Word word = JsonHelper.JsonDeserialize<Word>(result);
}
public static string url = "http://apis.baidu.com/apistore/tranlateservice/dictionary";
//要查询的字hello
public static string param = "query=hello&from=en&to=zh";
/// <summary>
/// 发送HTTP请求
/// </summary>
/// <param name="url">请求的URL</param>
/// <param name="param">请求的参数</param>
/// <returns>请求结果</returns>
public static string request(string url, string param)
{
string strURL = url + '?' + param;
System.Net.HttpWebRequest request;
request = (System.Net.HttpWebRequest)WebRequest.Create(strURL);
request.Method = "GET";
// 添加header
request.Headers.Add("apikey", "ba21378cab17ddc20b7449f866b51842");
System.Net.HttpWebResponse response;
response = (System.Net.HttpWebResponse)request.GetResponse();
System.IO.Stream s;
s = response.GetResponseStream();
string StrDate = "";
string strValue = "";
StreamReader Reader = new StreamReader(s, Encoding.UTF8);
while ((StrDate = Reader.ReadLine()) != null)
{
strValue += StrDate + "\r\n";
}
return strValue;
}
------解决思路----------------------
dictionary 本质不支持序列化,你要自己实现,
http://blog.csdn.net/woaixiaozhe/article/details/7873582
------解决思路----------------------
dict_result并非string,你打算如何处理?抛弃?