In C# how can I deserialize this json when one field might be a string or an array of strings?

后端 未结 3 1258
南方客
南方客 2021-01-14 04:05

I have an asp.net-mvc website and i am reading in Json string from a Database. Here is the following json in a DB. It could look like this:

{\"description\"         


        
3条回答
  •  暖寄归人
    2021-01-14 04:22

    try

      var contacts = (new JavaScriptSerializer().DeserializeObject(theAboveJsonString) as Dictionary)["contacts"];
    
      if (contacts is object[])
      {
          jobInfo.contacts = String.Join("; ", contacts as object[]);
      }
      else
      {
          jobInfo.contacts = contacts.ToString(); 
      }
    

    For reference see MSDN and here.

提交回复
热议问题