[英]How to get values out of JSON string array
I am passing the following array via jQuery ajax and JSON stringify... 我正在通过jQuery ajax和JSON stringify传递以下数组...
"sessionsArray":[{"eventNum":"15200","title":"Integrity"},{"eventNum":"15210","title":"Estate and Probate"}]
to ac#/.NET web service. 到ac#/。NET Web服务。 I need to loop over and extract the values but I'm having fuzzy brain today and cannot get the syntax correct.
我需要遍历并提取值,但是今天我脑子很模糊,无法正确使用语法。 Help please?
请帮助?
[WebMethod(EnableSession = true)]
public string saveRegistration(Dictionary <string, string> sessionsArray)
List<string[]> eventsList = new List<string[]>();
foreach (var eventItem in sessionsArray)
{
eventsList.Add(new[] { eventItem.Key, eventItem.Value });
}
Thanks. 谢谢。
我知道了。
Dictionary <string, string>[] sessionsArray
I guess I would define a model first for my objects like the following: 我想我首先要为我的对象定义一个模型,如下所示:
public class SessionsArray
{
public string eventNum { get; set; }
public string title { get; set; }
}
public class RootObject
{
public List<SessionsArray> sessionsArray { get; set; }
}
Then I would parse the json with JSON.NET: 然后,我将使用JSON.NET解析json:
string json = "{'sessionsArray':[{'eventNum':'15200','title':'Integrity'},{'eventNum':'15210','title':'Estate and Probate'}]}";
var root = JsonConvert.DeserializeObject<RootObject>(json);
You can use Newtonsoft.Json as follow : 您可以按以下方式使用Newtonsoft.Json:
var jsonData = JObject.Parse(sessionArray); var jsonData = JObject.Parse(sessionArray); Then you have JObject.
然后,您有了JObject。 You can cast this to JArray and you can use for loop to get data.
您可以将其强制转换为JArray,并可以使用for循环获取数据。
for(int i = 0 ; i<(JArray)jsonData.Count;i++)
{
var data = jsonData[i];
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.