0

I am passing the following array via jQuery ajax and JSON stringify...

"sessionsArray":[{"eventNum":"15200","title":"Integrity"},{"eventNum":"15210","title":"Estate and Probate"}]

to a c#/.NET web service. 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.

Connie DeCinko
  • 996
  • 5
  • 19
  • 39
  • possible duplicate of [Deserialize JSON with C#](http://stackoverflow.com/questions/7895105/deserialize-json-with-c-sharp) – TyCobb Jan 23 '15 at 17:41
  • Sorry, the only thing the same is the JSON string. I still don't see how to handle when the value is passed into my web service. I see some suggest using Dictionary, others something else. No one seems to agree. – Connie DeCinko Jan 23 '15 at 17:56
  • Can you post your Javascript code please. – Oday Fraiwan Jan 23 '15 at 18:05

3 Answers3

0

I got it to work.

Dictionary <string, string>[] sessionsArray
Connie DeCinko
  • 996
  • 5
  • 19
  • 39
0

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:

string json = "{'sessionsArray':[{'eventNum':'15200','title':'Integrity'},{'eventNum':'15210','title':'Estate and Probate'}]}";
var root = JsonConvert.DeserializeObject<RootObject>(json);
WellerEE
  • 338
  • 2
  • 5
0

You can use Newtonsoft.Json as follow :

var jsonData = JObject.Parse(sessionArray); Then you have JObject. You can cast this to JArray and you can use for loop to get data.

for(int i = 0 ; i<(JArray)jsonData.Count;i++)
{
var data = jsonData[i];
}
OnurBulbul
  • 433
  • 5
  • 9