使用C#从URL解析JSON数组。但是有" - "在班级名称中,你怎么能这样做?
示例:
public void gettimetable(string s) {
string get = getTimeTable(s);
JObject o = JObject.Parse(get);
var obj = JsonConvert.DeserializeObject<RootObject>(get);
TextBox1.Text = (obj.MyClass.ToString());
// TextBox1.Text=(obj.class_room[0].ToString());
}
修改
这是我尝试的代码。但是我没有把清单拿回来。
{{1}}
答案 0 :(得分:0)
您可以使用Json.NET属性使用JsonProperty
轻松实现此目的:
static void Main(string[] args)
{
var json = "{\"my-class\": [{\"class_name\": \"1H\",
\"class_room\": \"201aTD\",
\"day\": \"26\",
\"month\": \"10\",
\"period\": \"2\",
\"subject\": \"IF\",
\"teacher\": \"J04\",
\"year\": \"2015\"}]}";
Console.WriteLine(JsonConvert.DeserializeObject<RootObject>(json));
}
public class RootObject
{
[JsonProperty("my-class")]
public List<MyClass> MyClass { get; set; }
}
public class MyClass
{
[JsonProperty("class_name")]
public string ClassName { get; set; }
[JsonProperty("class_room")]
public string ClassRoom { get; set; }
[JsonProperty("day")]
public string Day { get; set; }
[JsonProperty("month")]
public string Month { get; set; }
[JsonProperty("period")]
public string Period { get; set; }
[JsonProperty("subject")]
public string Subject { get; set; }
[JsonProperty("teacher")]
public string Teacher { get; set; }
[JsonProperty("year")]
public string Year { get; set; }
}