sir i have one json i want deserlize this json how will i do iam try out but "Type 'System.String' is not supported for deserialization of an array." error coming here is my code : in cs file

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Web.Script.Serialization;
using System.Runtime.Serialization.Json;
using System.Net;
using System.Text;
using System.IO;
using System.Collections.Generic;


public partial class ProductType_MovieDetails : System.Web.UI.Page
{
    public SqlConnection con;
    public SqlCommand com;
  //  public SqlDataAdapter sda;
    public SqlDataReader reader;
    protected void Page_Load(object sender, EventArgs e)
    {
        con = new SqlConnection(ConfigurationManager.AppSettings["BigFlix_product"]);

        StringBuilder sb = new StringBuilder();
        byte[] buf = new byte[8192];
        //com = new SqlCommand("Bigflix_GetProductType", con);
        //com.CommandType = CommandType.StoredProcedure;
        //con.Open();
        //reader = com.ExecuteReader();
        //if (reader.HasRows)
        //{
        //    while (reader.Read())
        //    {
                sb.Length = 0;
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create("");
                HttpWebResponse responce = (HttpWebResponse)request.GetResponse();
                Stream resstream = responce.GetResponseStream();
                string tempstring = null;
                int count = 0;
                do
                {
                    count = resstream.Read(buf, 0, buf.Length);
                    if (count != 0)
                    {

                        tempstring = Encoding.ASCII.GetString(buf, 0, count);
                        sb.Append(tempstring);
                    }
                }
                while (count > 0);
                JavaScriptSerializer ser = new JavaScriptSerializer();
                List<Class2> result = ser.Deserialize<List<Class2>>(sb.ToString());

                foreach (var item in result)
                {
                    Response.Write("actor:" + item.actor + "<br/>");
                    Response.Write("actorlist:" + item.actorlist + "<br/>");
                    Response.Write("actors:" + item.actors + "<br/>");
                    Response.Write("personName:" + item.personName + "<br/>");
                    Response.Write("bannerId:" + item.bannerId + "<br/>");
                    Response.Write("bannerName:" + item.bannerName + "<br/>");
                    Response.Write("certificateofviewing:" + item.certificateofviewing + "<br/>");
                    Response.Write("channelName:" + item.channelName + "<br/>");
                    Response.Write("channelType:" + item.channelType + "<br/>");
                    Response.Write("contentUrl:" + item.contentUrl + "<br/>");
                    Response.Write("contentUrl2:" + item.contentUrl2 + "<br/>");
                    Response.Write("contentUrl3:" + item.contentUrl3 + "<br/>");
                    Response.Write("contentUrl4:" + item.contentUrl4 + "<br/>");
                    Response.Write("contentUrl5:" + item.contentUrl5 + "<br/>");
                    Response.Write("contentUrl6:" + item.contentUrl6 + "<br/>");
                    Response.Write("description:" + item.description + "<br/>");
                    Response.Write("director:" + item.director + "<br/>");
                    Response.Write("directorList:" + item.directorList + "<br/>");
                    Response.Write("directors:" + item.directors + "<br/>");
                    Response.Write("duration:" + item.duration + "<br/>");
                    Response.Write("episodename:" + item.episodename + "<br/>");
                    Response.Write("episodeno:" + item.episodeno + "<br/>");
                    Response.Write("free:" + item.free + "<br/>");
                    Response.Write("freeFlag:" + item.freeFlag + "<br/>");
                    Response.Write("fullStory:" + item.fullStory + "<br/>");
                    Response.Write("genre:" + item.genre + "<br/>");
                    Response.Write("genreList:" + item.genreList + "<br/>");
                    Response.Write("genres:" + item.genres + "<br/>");
                    //  Response.Write("personName:" + item.personName + "<br/>");
                    Response.Write("id:" + item.id + "<br/>");
                    Response.Write("imageUrl:" + item.imageUrl + "<br/>");
                    Response.Write("isSynopsys:" + item.isSynopsys + "<br/>");
                    Response.Write("language:" + item.language + "<br/>");
                    Response.Write("lowBitrateContentUrl:" + item.lowBitrateContentUrl + "<br/>");
                    Response.Write("lyricsList:" + item.lyricsList + "<br/>");
                    Response.Write("musicDirector:" + item.musicDirector + "<br/>");
                    Response.Write("name:" + item.name + "<br/>");
                    Response.Write("producerList:" + item.producerList + "<br/>");
                    Response.Write("productId:" + item.productId + "<br/>");
                    Response.Write("productMasterId:" + item.productMasterId + "<br/>");
                    Response.Write("productMasterName:" + item.productMasterName + "<br/>");
                    Response.Write("productName:" + item.productName + "<br/>");
                    Response.Write("productTypeId:" + item.productTypeId + "<br/>");
                    Response.Write("productTypeName:" + item.productTypeName + "<br/>");
                    Response.Write("rating:" + item.rating + "<br/>");
                    Response.Write("releaseYear:" + item.releaseYear + "<br/>");
                    Response.Write("shortChannelName:" + item.shortChannelName + "<br/>");
                    Response.Write("showGoodName:" + item.showGoodName + "<br/>");
                    Response.Write("singerList:" + item.singerList + "<br/>");
                    Response.Write("synopsis:" + item.synopsis + "<br/>");
                    Response.Write("synopsisForPartner:" + item.synopsisForPartner + "<br/>");
                    Response.Write("timeDuration:" + item.timeDuration + "<br/>");
                    Response.Write("views:" + item.views + "<br/>");







                }


           // }


        }




    }

class2.file code:
public class Class2
{
    public string actor { get;set;}
    public string actorlist { get;set;}
    public string actors { get;set;}
    public string personName { get;set;}
    public string bannerId { get;set;}
    public string bannerName { get;set;}
    public string certificateofviewing { get;set;}
    public string channelName { get;set;}
    public string channelType { get;set;}
    public string contentUrl { get;set;}
    public string contentUrl2 { get;set;}
    public string contentUrl3 { get;set;}
    public string contentUrl4 { get; set;}
    public string contentUrl5 { get;set;}
    public string contentUrl6 { get;set;}
    public string description { get;set;}
    public string director { get;set;}
    public string directorList { get;set;}
    public string directors { get;set;}
    public string duration { get;set;}
    public string episodename { get;set;}
    public string episodeno { get;set;}
    public string free { get;set;}
    public string freeFlag { get;set;}
    public string fullStory { get;set;}
    public string genre { get;set;}
    public string genreList { get;set;}
    public string genres { get;set;}
    //public string personName { get;set;}
    public string id { get;set;}
    public string imageUrl { get;set;}
    public string isSynopsys { get;set;}
    public string language { get;set;}
    public string lowBitrateContentUrl { get;set;}
    public string lyricsList { get;set;}
    public string musicDirector { get;set;}
    public string name { get;set;}
    public string producerList { get;set;}
    public string productId { get;set;}
    public string productMasterId { get;set;}
    public string productMasterName { get;set;}
    public string productName { get;set;}
    public string productTypeId { get;set;}
    public string productTypeName { get;set;}
    public string rating { get;set;}
    public string releaseYear { get;set;}
    public string shortChannelName { get;set;}
    public string showGoodName { get;set;}
    public string singerList { get;set;}
    public string synopsis { get;set;}
    public string synopsisForPartner { get;set;}
    public string timeDuration { get;set;}
    public string views { get;set;}
}

n my json is :

[
    {
        "actor": "Bindu,Amitabh Bachchan,Asrani,Jaya Bhaduri,A.K.Hangal",
        "actorList": [
            "Bindu",
            "Amitabh Bachchan",
            "Asrani",
            "Jaya Bhaduri",
            "A.K.Hangal"
        ],
        "actors": [
            {
                "personName": "Bindu"
            },
            {
                "personName": "Amitabh Bachchan"
            },
            {
                "personName": "Asrani"
            },
            {
                "personName": "Jaya Bhaduri"
            },
            {
                "personName": "A.K.Hangal"
            }
        ],
        "bannerId": 0,
        "bannerName": "",
        "certificateofviewing": "",
        "channelName": "Shemaroo Entertainment Pvt Ltd.",
        "channelType": "",
        "channelname": "Shemaroo Entertainment Pvt Ltd.",
        "contentUrl": "null",
        "contentUrl2": "null",
        "contentUrl3": "trailers/Abhimaan_176x144.mp4",
        "contentUrl4": "null",
        "contentUrl5": "null",
        "contentUrl6": "",
        "description": "Subir (Amitabh Bachchan) is a professional singer whose career is soaring. He does not plan to marry,until he meets Uma (Jaya Bhaduri),a sweet village girl who also sings. Subir falls in love with Uma and marries her. He returns to Mumbai with his new bride. Subir continues as a singer and also fosters Uma's singing career. His career falters,however,just as Uma's singing career begins to thrive. Eventually,she becomes more famous than her husband,sparking jealousy from Subir. His pride and jealousy tear the marriage apart. The question becomes whether Subir can overcome his jealousy.",
        "director": "Hrishikesh Mukherjee",
        "directorList": [
            "Hrishikesh Mukherjee"
        ],
        "directors": [
            {
                "personName": "Hrishikesh Mukherjee"
            }
        ],
        "duration": "2:00:00",
        "episodename": "",
        "episodeno": 0,
        "free": false,
        "freeFlag": false,
        "fullStory": "Subir (Amitabh Bachchan) is a professional singer whose career is soaring. He does not plan to marry,until he meets Uma (Jaya Bhaduri),a sweet village girl who also sings. Subir falls in love with Uma and marries her. He returns to Mumbai with his new bride. Subir continues as a singer and also fosters Uma's singing career. His career falters,however,just as Uma's singing career begins to thrive. Eventually,she becomes more famous than her husband,sparking jealousy from Subir. His pride and jealousy tear the marriage apart. The question becomes whether Subir can overcome his jealousy.",
        "genre": "Drama",
        "genreList": [
            "Drama"
        ],
        "genres": [
            {
                "personName": "Drama"
            }
        ],
        "id": 3,
        "imageUrl": "",
        "imageurl": "",
        "isSynopsys": "YES",
        "language": "Hindi",
        "lowBitrateContentUrl": "null",
        "lyricstList": [
            "Pawan Kumar"
        ],
        "musicDirector": [
            "S.D.Burman"
        ],
        "name": "Abhimaan",
        "producerList": [],
        "productId": 3,
        "productMasterId": 2,
        "productMasterName": "Abhimaan",
        "productName": "Abhimaan",
        "productTypeId": 1,
        "productTypeName": "Movie",
        "rating": 4,
        "releaseYear": "1973",
        "releaseyear": "1973",
        "shortChannelName": "Shemaroo En..",
        "showGoodName": "Movies ",
        "singerList": [],
        "synopsis": "Subir (Amitabh Bachchan) is a professional singer whose career is soaring. He does not plan to marry,until he meets Uma (Jaya Bhaduri),a sweet village girl who also sings. Subir falls in love with Uma and marries her. He returns to Mumbai with his new bride. Subir continues as a singer and also fosters Uma's singing career. His career falters,however,just as Uma's singing career begins to thrive. Eventually,she becomes more famous than her husband,sparking jealousy from Subir. His pride and jealousy tear the marriage apart. The question becomes whether Subir can overcome his jealousy.",
        "synopsisForPartner": "Subir (Amitabh Bachchan) is a professional singer whose career is soaring. He does not plan to marry,until he meets Uma (Jaya Bhaduri),a sweet village girl who also sings. Subir falls in love with Uma and marries her. He returns to Mumbai with his new bride. Subir continues as a singer and also fosters Uma's singing career. His career falters,however,just as Uma's singing career begins to thrive. Eventually,she becomes more famous than her husband,sparking jealousy from Subir. His pride and jealousy tear the marriage apart. The question becomes whether Subir can overcome his jealousy.",
        "timeDuration": "",
        "views": 423541
    }
]

please friends help me out thank you in advance

Dani AI

Generated

As ' JSON shows, several properties are actual arrays or non-string primitives (for example actorList is an array, actors is an array of objects, bannerId, id, rating are numbers, and free is a boolean). The error "Type 'System.String' is not supported for deserialization of an array." happens because the C# model declared those members as string while the JSON provides arrays or other types. The fix is to make the C# model match the JSON types (use List<T>/arrays for JSON arrays, proper numeric/bool types for numbers/booleans, and a small class for nested objects).

Example (DataContractJsonSerializer approach that does not require Json.NET):

using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using System.IO;
using System.Text;
using System.Collections.Generic;

[DataContract]
public class Person
{
  [DataMember(Name="personName")]
  public string PersonName { get; set; }
}

[DataContract]
public class Movie
{
  [DataMember(Name="actor")]
  public string Actor { get; set; }

  [DataMember(Name="actorList")]
  public List<string> ActorList { get; set; }

  [DataMember(Name="actors")]
  public List<Person> Actors { get; set; }

  [DataMember(Name="bannerId")]
  public int BannerId { get; set; }

  [DataMember(Name="free")]
  public bool Free { get; set; }

  [DataMember(Name="id")]
  public int Id { get; set; }

  [DataMember(Name="views")]
  public long Views { get; set; }
}

// deserialization:
var bytes = Encoding.UTF8.GetBytes(jsonString);
using (var ms = new MemoryStream(bytes))
{
  var ser = new DataContractJsonSerializer(typeof(List<Movie>));
  var movies = (List<Movie>)ser.ReadObject(ms);
}

Troubleshooting notes: match JSON property names exactly (or use DataMember(Name="...") for mapping), use nullable types (int?) where fields can be missing/null, watch for literal strings such as "null" in the JSON (these stay strings), and double-check JSON keys for typos/duplicate casing (e.g. lyricsList vs lyricstList, imageUrl vs imageurl) so the correct property receives data. 's suggestion of Json.NET would simplify this mapping, but with only framework classes the DataContractJsonSerializer or correct List<T>/nested-class types resolve the reported error.

Recommended Answers

All 3 Replies

Hi there,

i've always used JSON.NET in my projects, and for me it was very successful and easy to use.

The site has some good example too, and you can find a lot more on the web by searching it.

Link:

Hope it helps.

Hi there,

i've always used JSON.NET in my projects, and for me it was very successful and easy to use.

The site has some good example too, and you can find a lot more on the web by searching it.

Link:

Hope it helps.

thank you friend for reply but my sir told me without using json.net you shall desrlize json

This Article will expalin , how to create REST service which work on JSON data format. Then It will explain about how to perform HTTP GET and HTTP POST operation from a SilverLight client on a REST service on JSON Data Format.

Objective:

1.
How to create REST service for JSON request and response format.
2.
How to create SilverLight Client, which will consume a REST service on JSON data format.
3.
How to POST data in JSON format from SilverLight client.

Pre Requisite

Reader should have basic knowledge of REST services. For more on REST read my other articles.

Working explanation

Here, I am trying to insert data in static list and fetch it back at SilverLight client.

For Solution follow this link

thank you sir your valuable reply but this json iam doing in as.net 2.0 n i know it is deserlize using javascriptserilazer class but how it's work that don't know...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.