c#中如何改变json格式数据的某个值
2个回答
展开全部
我之前做过网络和桌面应用的数据对接,你看看
/*
1、对象集合
*/
[DataContract]
public class Items
{
[DataMember]
public List<ddd> ddd{get;set;}
public Items()
{
ddd = new List<ddd>();
}
//把字符串转换为对象
public static Items FormJson(string json)
{
try
{
System.Runtime.Serialization.Json.DataContractJsonSerializer a = new System.Runtime.Serialization.Json.DataContractJsonSerializer(typeof(Items));
using (MemoryStream stream = new MemoryStream(Encoding.Unicode.GetBytes(json)))
{
return (Items)a.ReadObject(stream);
}
}
catch (Exception)
{
return null;
}
}
}
/*
简单的对象
*/
[DataContract]
public class ddd
{
[DataMember]
public int cid{get;set;}
[DataMember]
public int pid { get; set; }
[DataMember]
public int oid { get; set; }
[DataMember]
public string view_type { get; set; }
[DataMember]
public string status { get; set; }
[DataMember]
public string name { get; set; }
[DataMember]
public string eng_name { get; set; }
[DataMember]
public string ctpl { get; set; }
[DataMember]
public string url { get; set; }
}
/*
2、从HTTP请求中得到数据
*/
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("http://www.bursonchurch.net//index.php?Interface-index-keyt-123-act-catelist.html");
request.Timeout = 5000;
request.Method = "GET";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Console.WriteLine("内容类型:"+response.ContentType);
Console.WriteLine("内容长度:"+response.ContentLength);
Console.WriteLine("服务器名:"+response.Server);
Console.WriteLine("资源的URI:"+response.ResponseUri);
Console.WriteLine("HTTP协议版本:" + response.ProtocolVersion);
Console.WriteLine("相应状态:"+response.StatusCode);
Console.WriteLine("相应方法:"+response.Method);
//头信息
for (int i = 0; i < response.Headers.Count; ++i)
{
Console.WriteLine("\nHeader Name:{0},----------Value:{1}", response.Headers.Keys[i], response.Headers[i]);
}
StreamReader sr = new StreamReader(response.GetResponseStream());
string jsonstr = sr.ReadToEnd();
//例子 :假设得到的json数据 下面序列化为对象
string test = "{\"ddd\":[{\"cid\":\"1\",\"pid\":\"0\",\"oid\":\"0\",\"view_type\":\"0\",\"status\":\"1\",\"name\":\"西科推荐\",\"eng_name\":\"\",\"ctpl\":\"\",\"ctitle\":\"\",\"ckeywords\":\"\",\"cdescription\":\"\",\"url\":\"http:\\/\\/www.bursonchurch.net\\/index.php?Cate-index-cid-1.html\"},{\"cid\":\"2\",\"pid\":\"0\",\"oid\":\"2\",\"view_type\":\"0\",\"status\":\"1\",\"name\":\"校园活动\",\"eng_name\":\"\",\"ctpl\":\"\",\"ctitle\":\"\",\"ckeywords\":\"\",\"cdescription\":\"\",\"url\":\"http:\\/\\/www.bursonchurch.net\\/index.php?Cate-index-cid-2.html\"},{\"cid\":\"3\",\"pid\":\"0\",\"oid\":\"2\",\"view_type\":\"0\",\"status\":\"1\",\"name\":\"帅哥\",\"eng_name\":\"\",\"ctpl\":\"\",\"ctitle\":\"\",\"ckeywords\":\"动物世界keword\",\"cdescription\":\"动物世界s\",\"url\":\"http:\\/\\/www.bursonchurch.net\\/index.php?Cate-index-cid-3.html\"},{\"cid\":\"4\",\"pid\":\"0\",\"oid\":\"4\",\"view_type\":\"1\",\"status\":\"1\",\"name\":\"静物\",\"eng_name\":\"\",\"ctpl\":\"\",\"ctitle\":\"\",\"ckeywords\":\"\",\"cdescription\":\"\",\"url\":\"http:\\/\\/www.bursonchurch.net\\/index.php?Cate-index-cid-4.html\"},{\"cid\":\"5\",\"pid\":\"0\",\"oid\":\"5\",\"view_type\":\"1\",\"status\":\"1\",\"name\":\"美女\",\"eng_name\":\"\",\"ctpl\":\"\",\"ctitle\":\"\",\"ckeywords\":\"\",\"cdescription\":\"\",\"url\":\"http:\\/\\/www.bursonchurch.net\\/index.php?Cate-index-cid-5.html\"}]}";
//第一种转
JavaScriptSerializer j = new JavaScriptSerializer();
Items ii = new Items();
ii = j.Deserialize<Items>(jsonstr);
//第二种也行
Items it = Items.FormJson(jsonstr);
response.Close();
Console.ReadKey();
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询