Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7794167
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T22:42:22+00:00 2026-06-01T22:42:22+00:00

I have an asp.net-mvc website and i am reading in Json string from a

  • 0

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": "Test", "contacts": ["joe@gmail.com", "bill@yahoo.com"], "enabled": true}

or this:

{"description": "Test", "contacts": "joe@gmail.com, bill@yahoo.com", "enabled": true}

so as you can see, the contacts field is either:

  1. a string (with items separated by commas)
  2. an array of strings.

I want to convert to this class:

public class MyJob
{
    public string description;
    public string[] contacts;
    public string enabled;
}

when i try to assign just to a string (changing the above to this: public string contacts;
) using the JavascriptSerializer():

var serializer = new JavaScriptSerializer();
string contacts = serializer.Deserialize<MyJob>(theAboveJsonString).contacts;

I get this error in the cases where its an array: Type ‘System.String’ is not supported for deserialization of an array.

what is the best way to go about deserializing this to handle the case of:

  1. a string
  2. an array of strings.

for the contact field. I am happy to put any conditional logic needed . .

I tried this:

  var contacts = serializer.Deserialize<MyJob>(theAboveJsonString).contacts;
        if (contacts is string)
        {
            jobInfo.contacts = contacts;
        }
        else
        {
            jobInfo.contacts = String.Join("; ", contacts );
        }

but that didn’t seem to fix as i am still getting the error above when its an array

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T22:42:23+00:00Added an answer on June 1, 2026 at 10:42 pm

    try

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

    For reference see MSDN and here.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my asp.net-mvc website I have a field that usually has a string (from
I have an asp.net mvc website that returns a JSON result to certain pages
I have the following view definition in my asp.net mvc website: <% Using Ajax.BeginForm(UsrCtlChangePassword,
I have just set up a new ASP.NET MVC website and I would like
i have an asp.net-mvc website and i want to merge in some functionality from
i have as asp.net-mvc website and we are migrated a popup from using PartialView()
I have an existing Asp.Net MVC Website and I would also like to provide
i have a asp.net mvc website and the volume of traffic is increasing. I
I have an ASP.NET MVC website that works in tandem with a Windows Service
I have an ASP.Net MVC 3 Website hosted on a shared Windows hosting package.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.