0

I searched for an answer, found something similar but not exact my case.

So, I have a URL that returns JSON when i request. Json is like this:

    {"12458":{"id":"12458","name":"Deccal","short":"<p>H\u0259yat\u0131n\u0131n s\u0131x\u0131nt\u0131l\u0131 vaxtlar\u0131n\u0131 ya\u015fayan Duygu, x\u0259st\u0259xanadan 
&ccedil;\u0131xd\u0131qdan sonra t\u0259sad&uuml;f\u0259n Nurdan ad\u0131nda...<\/p>","full":"<p>H\u0259yat\u0131n\u0131n s\u0131x\u0131nt\u0131l\u0131 vaxtlar\u0131n\u0131 ya\u015fayan 
Duygu, x\u0259st\u0259xanadan &ccedil;\u0131xd\u0131qdan sonra t\u0259sad&uuml;f\u0259n Nurdan ad\u0131nda, sirli v\u0259 o q\u0259d\u0259r d\u0259 varl\u0131 qad\u0131nla tan\u0131\u015f 
olur. Nurdan\u0131n maddi v\u0259 m\u0259n\u0259vi k&ouml;m\u0259yini, ehtiyac\u0131 oldu\u011fu &uuml;&ccedil;&uuml;n q\u0259bul ed\u0259n Duy\u011funun h\u0259yat\u0131 inan\u0131lmaz 
d\u0259yi\u015fir. \u018fn yax\u0131n dostu Asl\u0131 da bu v\u0259ziyy\u0259td\u0259n raz\u0131d\u0131r. Amma sonradan Duygunun yerl\u0259\u015fdiyi q\u0259rib evd\u0259 q\u0259rib\u0259 
hadis\u0259l\u0259r ba\u015f verm\u0259y\u0259 ba\u015flay\u0131r. Duygu haradan g\u0259ldiyi bilinm\u0259y\u0259n s\u0259sl\u0259r e\u015fidir v\u0259 k&ouml;lg\u0259l\u0259r 
i&ccedil;ind\u0259 psixologiyas\u0131 daha da pisl\u0259\u015fir. Dig\u0259r t\u0259r\u0259fd\u0259n d\u0259, x\u0259st\u0259xanada iki q\u0131z\u0131 da yeni x\u0259b\u0259rl\u0259r 
g&ouml;zl\u0259yir.<\/p>\n<p>Rejissor:&nbsp;&Ouml;zg&uuml;r Bakar&nbsp;<\/p>\n<p>Ssenarist:&nbsp;Alper K\u0131v\u0131lc\u0131m, &Ouml;zg&uuml;r 
Bakar&nbsp;<\/p>","dates":[{"place":"\"Nizami\" kinoteatr\u0131","dates":"20 avqust - 10 sentyabr","prices":["14:45 : 4 AZN","18:50 : 7 AZN"]}]},"11949":{"id":"11949","name":"Kocan kadar 
konus","short":"<p>Filmd\u0259 h\u0259yat\u0131n\u0131n do\u011fru insan\u0131n\u0131 tapa bilm\u0259y\u0259n bir qad\u0131n\u0131n hekay\u0259sind\u0259n b\u0259hs 
edilir<\/p>","full":"<p>\u015e\u0259bn\u0259m Burcuo\u011flunun eyni adl\u0131 roman\u0131 \u0259sas\u0131nda &ccedil;\u0259kilmi\u015f filmd\u0259 h\u0259yat\u0131n\u0131n do\u011fru 
insan\u0131n\u0131 tapa bilm\u0259y\u0259n bir qad\u0131n\u0131n hekay\u0259sind\u0259n b\u0259hs edilir.<\/p>\n<p>Rollarda:&nbsp;Ejqi Mola,&nbsp;Murad 
Y\u0131ld\u0131r\u0131m<\/p>","dates":[{"place":"28 Cinema","dates":"15 avqust - 11 sentyabr","prices":["15:40 : 5 AZN"]}]},...

There are lots of objects with structure like:

{
string id,
string name,
string short,
string full,
string dates{
string place,
string dates,
string[] prices
}
}.

My Class

[DataContract]
public class FilmList
{
    [DataMember]
    public List<Film> films { get; set; }
    public class Film
    {
        [DataMember(Name = "id")]
        public string id { get; set; }
        [DataMember(Name = "name")]
        public string name { get; set; }
        [DataMember(Name = "short")]
        public string s { get; set; }
        [DataMember(Name = "full")]
        public string full { get; set; }
        [DataMember(Name = "dates")]
        public Dates dates { get; set; }
        public class Dates
        {
            [DataMember(Name = "place")]
            public string place { get; set; }
            [DataMember(Name = "dates")]
            public string dates { get; set; }
            [DataMember(Name = "prices")]
            public List<string> prices { get; set; }
        }
    }
}

Code

DataContractJsonSerializer j = new DataContractJsonSerializer(typeof(FilmList));
        //Film f = null;
        JavaScriptSerializer ser = new JavaScriptSerializer();
        using (var s = resp.GetResponseStream())//var sr = new StreamReader(resp.GetResponseStream()))
        {
            //var s = ser.Serialize(sr.ReadToEnd());
            //f = (Film)j.ReadObject(s);
            FilmList f = (FilmList)j.ReadObject(s);//ser.Deserialize<FilmList>(sr.ReadToEnd());
            //var json = ser.Deserialize<FilmList>(sr.ReadToEnd());

        }

        resp.Close();

I tried both DataContractJsonSerializer and JavaScriptSerializer, and both return me null. I think it might be because JSON doesn't have a header element - just list of objects, but i'm not sure.

1 Answer 1

1

this is because your json is not correctly formatted, Validate your Json on http://jsonlint.com/

try to parse below mentioned Json, it will parse.

{
    "12458": {
        "id": "12458",
        "name": "Deccal",
        "short": "<p>Həyatının sıxıntılı vaxtlarını yaşayan Duygu, xəstəxanadan &ccedil;ıxdıqdan sonra təsad&uuml;fən Nurdan adında...</p>",
        "full": "<p>Həyatının sıxıntılı vaxtlarını yaşayan Duygu, xəstəxanadan &ccedil;ıxdıqdan sonra təsad&uuml;fən Nurdan adında, sirli və o qədər də varlı qadınla tanış olur. Nurdanın maddi və mənəvi k&ouml;məyini, ehtiyacı olduğu &uuml;&ccedil;&uuml;n qəbul edən Duyğunun həyatı inanılmaz dəyişir. Ən yaxın dostu Aslı da bu vəziyyətdən razıdır. Amma sonradan Duygunun yerləşdiyi qərib evdə qəribə hadisələr baş verməyə başlayır. Duygu haradan gəldiyi bilinməyən səslər eşidir və k&ouml;lgələr i&ccedil;ində psixologiyası daha da pisləşir. Digər tərəfdən də, xəstəxanada iki qızı da yeni xəbərlər g&ouml;zləyir.</p>\n<p>Rejissor:&nbsp;&Ouml;zg&uuml;r Bakar&nbsp;</p>\n<p>Ssenarist:&nbsp;Alper Kıvılcım, &Ouml;zg&uuml;r Bakar&nbsp;</p>",
        "dates": [
            {
                "place": "\"Nizami\" kinoteatrı",
                "dates": "20 avqust - 10 sentyabr",
                "prices": [
                    "14:45 : 4 AZN",
                    "18:50 : 7 AZN"
                ]
            }
        ]
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

thank you. It says that json is valid. so, it's not public Dates dates { get; set; } but public List<Dates> dates { get; set; }, right?
yes that's right, because in your json u have array of date objects. Kindly mark this as answer if helps

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.