2024-11-07 06:11:36
在C#中怎么解析JSON数据,并获取到其中的值,案例如下:
#JSON数据为:{"phantom":true,"id":"ext-record-10","data":{"MID":1019,"Name":"aaccccc","Des":"cc","Disable":"启用","Remark":"cccc"}}。
#需要得到结果为{"MID":1019,"Name":"aaccccc","Des":"cc","Disable":"启用","Remark":"cccc"}的字符串。
功能代码:
using System.Runtime.Serialization.Json;
[Serializable]
public class InternalClass
{
public int MID;
public string Name;
public string Des;
public string Disable;
public string Remark;
}
[Serializable]
public class OuterClass
{
public bool phantom;
public string id;
public InternalClass data;
}
private void button2_Click(object sender, EventArgs e)
{
const string json = @"{""phantom"":true,
""id"":""ext-record-10"",
""data"":
{""MID"":1019,
""Name"":""aaccccc"",
""Des"":""cc"",
""Disable"":""启用"",
""Remark"":""cccc""}}";
DataContractJsonSerializer ser1 = new DataContractJsonSerializer(typeof(OuterClass));
using (MemoryStream ms = new MemoryStream(Encoding.Unicode.GetBytes(json)))
{
OuterClass foo1 = ser1.ReadObject(ms) as OuterClass;
}
}
2024-11-07 12:53:31
我在用WINFORM C#
2024-11-07 04:35:57
我在用WINFORM C#
http://msdn.microsoft.com/library/system.runtime.serialization.datacontractserializer.aspx
需要序列化的类使用[DataContractAttribute]特性,字段使用[DataMemberAttribute]特性
不太会看...
namespace WindowsFormsApplication2
{
[Serializable]
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
System.Web.Script.Serialization.JavaScriptSerializer jss = new System.Web.Script.Serialization.JavaScriptSerializer();
Dictionary dic = (Dictionary)jss.Deserialize(Temp, typeof(Dictionary>));
}
}
[Serializable]
public class JsonTmp
{
public string sFName = string.Empty;
}
}
对不起啊!还是看不明白
然后呢? 我想label3.text为sFName,label4.text为sLName在以上那个位置加
能私信你的q.Q吧!真心想请教你c#问题
2024-11-07 10:58:59
不行啊