我有一个.json文件,我想解析它并在页面中显示项目。 我创建了3个类叫: bultan_details bultan_new bultan_tele 这个代码用于解析它:
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班级
public class Bultan
{
public bultan_details Bultan_Details { get; set; }
public List<Bultan_New> BultanNews { get; set; }
public List<Bultan_Tele> BultanTelegram { get; set; }
}
但运行项目后我收到此错误:
(例如{&#34; name&#34;:&#34; value&#34;})正确反序列化。要修复此错误 将JSON更改为JSON对象(例如{&#34; name&#34;:&#34; value&#34;})或 将反序列化类型更改为数组或实现的类型 集合接口(例如ICollection,IList)就像List一样 从JSON数组反序列化。也可以添加JsonArrayAttribute 到类型强制它从JSON数组反序列化。路径 &#39; bultan_details&#39;,第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": ""
}
]
}
答案 0 :(得分:3)
您的bultan_details
与json
bultan_news
和bultan_tele
中的数组相同,您必须将其定义为dto类中的集合:
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; }
}
答案 1 :(得分:1)
我建议使用jsonutils.com从JSON文件创建对象类。
然后id使用您可以在NuGet包管理器中找到的Newtonsoft.json。以下代码对我来说很好 -
(0.49019607843137247, 1.0, 0.47754585705249841, 1.0)
答案 2 :(得分:0)
我建议您创建一个新对象序列化它并将其与您的输入文件合并。所以你会看到差异..