c#如何读取json中的值

我已经把Json读了进temp,
String temp ={"response":[{"sFName":"Tom","sLName":"Lai","position":"Manage","permission":"0"}]};
如何把temp中的sFname,sLname,position,permisstion取出来
我在用WINFORM C#
最新回答
纯家小可爱

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

首先,你这个不是一个正确的json格式字符串,正确:

String temp =[{"response":[{"sFName":"Tom","sLName":"Lai","position":"Manage","permission":"0"}]}];

你是想在客户端读取这个字符串吗?如果是你可以这样写;

<script>
var json_str=eval('<%=temp%>');
var j1=json_str[0];
$.each(j1,function(i,boj){
var sFname=j1.sFName;
alert(sFname);//其他依次类推
});

</script>
追问
我在用WINFORM C#
佐佐木惠理

2024-11-07 04:35:57

是web还是Winfrom?
1、添加引用
2、 System.Web.Script.Serialization.JavaScriptSerializer jss = new System.Web.Script.Serialization.JavaScriptSerializer();
jss.Serialize(_obj);//_obj必须实现[System.Serializable()]特性
追问
我在用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

取sFname 的方法是: temp.response.sFName;
其他的不用说了吧.
追问
不行啊