2017-03-23 5 views
2

私はそれを解析し、ページに項目を表示する.jsonファイルを持っています。これは私のBultanクラスASP.NETでPars jsonファイルC#

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

ですが、私はプロジェクトを実行した後に、このエラーが出る

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/>"); 
         } 
        } 

bultan_details bultan_new bultan_tele し、それを解析するため、このコード: 私はと呼ばれる3クラスを作成しました

(例:{"name": "value"})正しくデシリアライズします。このエラーを修正するには JSONオブジェクトをJSONオブジェクト(たとえば{"name": "value"})に変更するか、 デシリアライズされたタイプを配列または コレクションインターフェイス(ICollection、IListなど)を実装するタイプに変更します。リストは で、JSON配列から逆シリアル化できます。 JSON配列から強制的に非直列化するために、JsonArrayAttributeを型に に追加することもできます。パス 'bultan_details'、行1、位置19

と、これは私のJSONファイルの内容です:

{ 
    "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

ショーをあなたのJSON –

+0

を追加しましたJSONコンテンツ、申し訳ありません私のミスは、私は、JSONコンテンツを追加するのを忘れ –

答えて

3

bultan_detailsbultan_newsbultan_teleとしてjsonで同じ配列であるあなた、あなたが定義する必要があります

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; } 
} 
0

私はあなたがそれをシリアライズ新しいオブジェクトを作成し、自分の中でそれをマージすることをお勧め:あまりにもDTOクラスのコレクションとしてそれファイルを置く。違いを見ることができます。

1

私はjsonutils.comを使用して、JSONファイルからオブジェクトクラスを作成することをお勧めします。

次に、NuGet Package ManagerにあるNewtonsoft.jsonをIDで使用します。以下のコードは私のために正常に動作します - 私たちに

Bultan data = new Bultan(); 

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