110,534
社区成员
发帖
与我相关
我的任务
分享
{
"list": [
{
"account": "As",
"flag": "",
"id": "0f86ebbe-df69-11e1-b891-41d04d127205",
"password": "123456",
"regDate": ""
},
{
"account": "111111",
"flag": "",
"id": "36988ff4-dc46-11e1-a738-fc2be3f21384",
"password": "111111",
"regDate": ""
},
{
"account": "111111",
"flag": "251637313@qq.com",
"id": "402881e136103b560136103bcdf20001",
"password": "0B4E7A0E5FE84AD35FB5F95B9CEEAC79",
"regDate": "1"
},
{
"account": "222222",
"flag": "251637313@qq.com",
"id": "8a809762359e71aa01359e74296f0001",
"password": "E3CEB5881A0A1FDAAD01296D7554868D",
"regDate": "0"
},
{
"account": "777",
"flag": "777",
"id": "8a80d8ab389e119001389e11e1e50001",
"password": "777",
"regDate": "1"
}
]
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Script.Serialization;
using System.IO;
namespace TestWebServicePro
{
class JsonHelp
{
/// <summary>
/// 将对象转换为 JSON 字符串。
/// </summary>
/// <param name="obj">要序列化的对象。</param>
/// <returns>序列化的 JSON 字符串。</returns>
public static string JsonSerialize(object obj)
{
JavaScriptSerializer jsSerializer = new JavaScriptSerializer();
return jsSerializer.Serialize(obj);
}
/// <summary>
/// 将指定的 JSON 字符串转换为 T 类型的对象。
/// </summary>
/// <typeparam name="T">所生成对象的类型。</typeparam>
/// <param name="input">要进行反序列化的 JSON 字符串。</param>
/// <param name="def">反序列化失败时返回的默认值。</param>
/// <returns>反序列化的对象。</returns>
public static T JosnDeserialize<T>(string input, T def)
{
if (string.IsNullOrEmpty(input))
return def;
try
{
JavaScriptSerializer jsSerializer = new JavaScriptSerializer();
return jsSerializer.Deserialize<T>(input);
}
catch (InvalidOperationException)
{
return def;
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace TestWebServicePro
{
[Serializable()]
class Test
{
public string account { get; set; }
public string flag { get; set; }
public string id { get; set; }
public string password { get; set; }
public string regDate { get; set; }
public override string ToString()
{
return string.Format("account={0}, flag={1}, id={2},password={3},regDate={4}", account, flag, id, password, regDate);
}
}
}
private void button1_Click(object sender, EventArgs e)
{
WebReference.TestWebService ts = new WebReference.TestWebService();
string strJson = ts.getUserList();//这里的字符串就是通过java webservice返回的,已经贴在上面了
Test item = JsonHelp.JosnDeserialize<Test>(strJson, null);
if (item != null)
{
Console.WriteLine(item.ToString());
}
}
try
{
WebClient WebClientWeather = new WebClient();
Stream streamWeather = WebClientWeather.OpenRead("http://m.weather.com.cn/data/101120301.html");
StreamReader StreamReaderWeather = new StreamReader(streamWeather);
string strWeather = StreamReaderWeather.ReadToEnd();
System.Web.Script.Serialization.JavaScriptSerializer json = new System.Web.Script.Serialization.JavaScriptSerializer();
Dictionary<string, object> weatherData = (Dictionary<string, object>)json.DeserializeObject(strWeather);
Dictionary<string, object> weatherInfo = (Dictionary<string, object>)weatherData["weatherinfo"];
string strTemp = weatherInfo["temp1"].ToString();
string weatherText = weatherInfo["weather1"].ToString();
//[Serializable()] 改成
[System.Runtime.Serialization.DataContractAttribute]
class Test
{
[System.Runtime.Serialization.DataMemberAttribute] //每个属性上面都加上
public string account { get; set; }
//Main:
string output="{\"status\":100,\"info\":\"ok\",\"data\":{\"c_id\":\"12\",\"c_key\":\"a8ada1d9\",\"c_name\":\"\u5357\u4eac\u70fd\u706b\u79d1\u6280\u6709\u9650\u516c\u53f8\",\"b_key\":\"EDUSHI_11544\",\"n_key\":\"qe123Waq\",\"c_card\":\"<span></span>\",\"contact_person\":\"\u5f20\u4e09\",\"contact_phone\":\"800-8800787\",\"virtual_name\":\"fenghuo-oa\",\"virtual_addr\":\"172.16.50.207\",\"status\":100}}";
Product deserializedProduct = (Product)JavaScriptConvert.DeserializeObject(output, typeof(Product));
Console.WriteLine(deserializedProduct.status);
Console.WriteLine(deserializedProduct.data.c_card);
//Model:
public class Product
{
private int Status;
public int status
{
get { return Status; }
set { Status = value; }
}
private string Info;
public string info
{
get { return Info; }
set { Info = value; }
}
private StringData Data;
public StringData data
{
get { return Data; }
set { Data = value; }
}
}
//StringData类型就省略拉 太多属性