2

i have a .json file that i want to pars it and show items in page. i created 3 class called : bultan_details bultan_new bultan_tele and this codes for parsing it :

using (StreamReader r = new StreamReader(filePath))
                    {
                        var json = r.ReadToEnd();
                        var result = JsonConvert.DeserializeObject<Bultan>(json);

                        //var bultanDetails = result.Bultan_Details;
                        var news = result.BultanNews;
                        var telegram = result.BultanTelegram;
                        //Response.Write(bultanDetails.bultan_title + "<br/>");
                        foreach (var item in news)
                        {
                            Response.Write(item.description + "<br/><br/><br/>");
                        }
                        foreach (var tl in telegram)
                        {
                            Response.Write(tl.text + "<br/><br/><br/><br/>");
                        }
                    }

this is my Bultan class

public class Bultan
    {
        public bultan_details Bultan_Details { get; set; }
        public List<Bultan_New> BultanNews { get; set; }
        public List<Bultan_Tele> BultanTelegram { get; set; }
    }

but i get this error after running project :

(e.g. {"name":"value"}) to deserialize correctly. To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array. Path 'bultan_details', line 1, position 19.

and this is my json file content :

{
    "bultan_details": [
        {
            "export_url": "http://localhost/api/public",
            "bultan_title": "عنوان اول",
            "bultan_title2": "عنوان دوم",
            "bultan_title3": "عنوان سوم",
            "bultan_title4": "عنوان چهارم",
            "bultan_logo": "logo.jpg",
            "full_date": "بهمن 25، 1395",
            "morning_azan": "05:30",
            "noon_azan": "12:19",
            "sunset_azan": "18:02",
            "sunset": "17:43",
            "sunrise": "06:55"
        }
    ],
    "bultan_news": [
        {
            "id": 83773354,
            "title": "زمان واریز عیدی بازنشستگان فعلا مشخص نیست",
            "uptitle": "",
            "description": "MyContent",
            "source_name": "ایران آنلاین",
            "time": "1395-11-19 09:43:38",
            "copy_count": 20,
            "base_source": "خبرگزاری فارس",
            "news_group": "اخبار سازمان",
            "news_subject": "تست میشود 2323",
            "news_subject2": "امیر,نظر",
            "news_maker": "تست 25",
            "news_orientation": "negative",
            "news_template": "note",
            "notes": "خوب بود",
            "tone": "pro",
            "tools": "quip",
            "labels": "جالب,قشنگ",
            "another_sources": "کلید فارس,بام فارس"
        },
        {
            "id": 83442478,
            "title": "سایت استانی خبرگزاری فارس اردبیل رونمایی شد",
            "uptitle": "",
            "description": "Description",
            "content": "MyContent",
            "source_name": "ارس تبار",
            "time": "1395-11-16 22:01:00",
            "copy_count": 0,
            "base_source": "ارس تبار",
            "news_group": "",
            "news_subject": "",
            "news_subject2": "",
            "news_maker": "",
            "news_orientation": "",
            "news_template": "",
            "notes": "",
            "tone": "",
            "tools": "",
            "labels": "",
            "another_sources": ""
        },
        {
            "id": 83443525,
            "title": "Title",
            "uptitle": "",
            "description": "Description",
            "content": "MyContent",
            "source_name": "خبرگزاری مهر",
            "time": "1395-11-16 22:15:00",
            "copy_count": 1,
            "base_source": "خبرگزاری مهر",
            "news_group": "",
            "news_subject": "",
            "news_subject2": "",
            "news_maker": "",
            "news_orientation": "",
            "news_template": "",
            "notes": "",
            "tone": "",
            "tools": "",
            "labels": "",
            "another_sources": "تابناک کهکیلویه"
        },
        {
            "id": 83443417,
            "title": "Title",
            "uptitle": "",
            "description": "Description",
            "content": "Content",
            "source_name": "خبرگزاری ایرنا",
            "time": "1395-11-16 22:14:43",
            "copy_count": 1,
            "base_source": "خبرگزاری ایرنا",
            "news_group": "",
            "news_subject": "",
            "news_subject2": "",
            "news_maker": "",
            "news_orientation": "",
            "news_template": "",
            "notes": "",
            "tone": "",
            "tools": "",
            "labels": "",
            "another_sources": "خبرگزاری ایرنا"
        }
],
    "bultan_telegram": [
        {
            "id": 3919776484,
            "link": "https://t.me/Besuye_zohour/4977",
            "source_name": "️💥 بسـوی ظــهور 💥",
            "text": "MyText",
            "time": "1395-11-19 08:58:37",
            "base_source": "️💥 بسـوی ظــهور 💥",
            "news_group": "",
            "news_subject": "",
            "news_subject2": "",
            "news_maker": "",
            "notes": "",
            "tone": "",
            "tools": "",
            "labels": "",
            "another_sources": "",
            "news_orientation": ""
        },
        {
            "id": 3919776370,
            "link": "https://t.me/onlyshear/37507",
            "source_name": "ســــــــراۍ شــ؏ــــــر",
            "text": "MyText",
            "time": "1395-11-19 08:58:37",
            "base_source": "ســــــــراۍ شــ؏ــــــر",
            "news_group": "",
            "news_subject": "",
            "news_subject2": "",
            "news_maker": "",
            "notes": "",
            "tone": "",
            "tools": "",
            "labels": "",
            "another_sources": "",
            "news_orientation": ""
        }
    ]
}
2
  • 2
    Show to us your json Commented Mar 23, 2017 at 10:13
  • Added json content, sorry my mistake i forgot to add json content Commented Mar 23, 2017 at 10:18

3 Answers 3

3

Your bultan_details is the same array in json as bultan_news and bultan_tele, you have to define it as a collection in dto class too:

public class Bultan
{
    public List<bultan_details> Bultan_Details { get; set; }
    public List<Bultan_New> BultanNews { get; set; }
    public List<Bultan_Tele> BultanTelegram { get; set; }
}
Sign up to request clarification or add additional context in comments.

Comments

1

I'd suggest using jsonutils.com to create the object class from a JSON file.

Then id use Newtonsoft.json which you can find in the NuGet Package Manager. And the code below works fine for me -

Bultan data = new Bultan();

var file = Path.Combine(@"FILE PATH");
if (File.Exists(file))
{
    data = JsonConvert.DeserializeObject<Bultan>(File.ReadAllText(file));
}

Comments

0

i suggest you to create a new object serialize it and merge it with your input file. So you will see differences..

Comments

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.