问题描述
我对此很陌生,所以我真的希望有人能帮助我.
我的JSON文件如下所示(index3.json):
{"weeks":[{"week":1,"mothertext":"Det her er uge mor 1 mor","babytext":"Det her er uge uge 1 baby"},{"week":2, "mothertext":"Det her er uge 2 mor","babytext":"Det her er uge 2 mor"},{"week":3,"mothertext":"Det her er uge mor 2 mor","babytext" :请给她的3个孩子喂药"}]}
我想在Visual Studio中创建一个数组,以便可以从不同属性插入文本. IE. week.mothertext [1](或它的外观)将提供带有"Det her er uge 2 mor"的字符串,依此类推.
我必须承认,这是我在网上找到的代码所构造的错误代码.
Hi,
I''m very new to this, so I really hope there is somebody smart out there that will help me.
My JSON file looks like this (index3.json):
{"weeks": [{ "week": 1, "mothertext":"Det her er uge 1 mor", "babytext": "Det her er uge 1 baby" } ,{ "week": 2, "mothertext":"Det her er uge 2 mor", "babytext": "Det her er uge 2 baby" } ,{ "week": 3, "mothertext":"Det her er uge 3 mor", "babytext": "Det her er uge 3 baby" } ]}
I want to create an array in visual studio so its possible to insert the text from the different properties. I.e. week.mothertext[1] (or how it would look like) would deliver a string with "Det her er uge 2 mor" and so on.
This is my code which I must admit is losely constructed from code I have found on the web.
public InformationPage()
{
InitializeComponent();
}
public class weeksArray
{
public weekProperties[] weeklist { get; set; }
}
public class weekProperties
{
public int week { get; set; }
public string mothertext { get; set; }
public string babytext { get; set; }
}
private void myButton1_Click(object sender, RoutedEventArgs e)
{
var client = new WebClient();
client.OpenReadCompleted +=
( s, eargs ) = >
{
var serializer = new DataContractJsonSerializer(typeof(weeksArray));
var dataArray = (weeksArray)serializer.ReadObject(eargs.Result);
foreach(var obj in dataArray.weeks)
//This sort of works but doesnt allow me to retrieve the indidual objects like the
//the text for week: 3 only.
{
textBoxWeek0.TextWriter = textBoxWeek0.Text + obj.week;
}
};
var uri = new Uri("http://...../index3.json");
client.OpenReadAsync( uri );
}
推荐答案
class Program
{
static void Main(string[] args)
{
string json = "[{\"week\":\"1\",\"mothertext\":\"Det her er uge 1 mor\",\"babytext\":\"Det her er uge 1 baby\"},{\"week\":\"2\",\"mothertext\":\"Det her er uge 2 mor\",\"babytext\":\"Det her er uge 2 baby\"},{\"week\":\"3\",\"mothertext\":\"Det her er uge 3 mor\",\"babytext\":\"Det her er uge 3 baby\"}]";
List<Week> weeks = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Week>>(json);
foreach (Week week in weeks)
{
Console.WriteLine("Week {0} - {1} - {2}", week.week, week.mothertext, week.babytext);
}
Console.ReadLine();
}
}
public class Week
{
public string week { get; set; }
public string mothertext { get; set; }
public string babytext { get; set; }
}
使用lambda表达式过滤结果的示例
Example use of lambda expression to filter results
var filteredWeeks = weeks.Where(x => x.week == "2");
foreach (Week week in weeks)
{
Console.WriteLine("Week {0} - {1} - {2}", week.week, week.mothertext, week.babytext);
}
Console.WriteLine("Filtered");
foreach (Week filteredWeek in filteredWeeks)
{
Console.WriteLine("Week {0} - {1} - {2}", filteredWeek.week, filteredWeek.mothertext, filteredWeek.babytext);
}
Console.ReadLine();
这篇关于从文件夹到C#的JSON序列化.初学者!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!