简体   繁体   English

如何在C#中将Json对象转换为数组

[英]How to Convert Json Object to Array in C#

I am trying to convert a JSON object in to C# array. 我正在尝试将JSON对象转换为C#数组。

this is the JSon I Getfrom the Server webrequest response: 这是JSon I Getfrom the Server webrequest响应:

string result = sr.ReadToEnd(); // this line get me response 
result = {
    "subjects": [{
        "subject_id": 1,
        "subject_name": "test 1",
        "subject_class": 4,
        "subject_year": "2015",
        "subject_code": "t-1"
    },{
        "subject_id": 2,
        "subject_name": "test 2",
        "subject_class": 5,
        "subject_year": "",
        "subject_code": "t-2"
    }]
};

dynamic item = JsonConvert.DeserializeObject<object>(result);  

string iii = Convert.ToString(item["subjects"]);

I want to Get the Subjects and Save them in Array so i can use them for other purpose. 我想获取主题并将它们保存在数组中,以便我可以将它们用于其他目的。

I use these to Method but always got the empty values. 我使用这些方法但总是得到空值。

List<subjects> subject1 = (List<subjects>)JsonConvert.DeserializeObject(iii, typeof(List<subjects>));

and

subjects[] subject2 = JsonConvert.DeserializeObject<subjects[]>(iii);

Please Help Me to Solve this. 请帮我解决这个问题。

And my Subject Class is.. 我的学科班是......

class subjects
{
    public int id { get; set; }
    public string name { get; set; }
    public int class_name { get; set; }
    public string year { get; set; }
    public string code { get; set; }
}

The property names won't match as is, because you subjects class don't have the 'subject_' prefix the JSON object has. 属性名称不会按原样匹配,因为主题类没有JSON对象具有的'subject_'前缀。 Easiest fix is to change your property names as shown in Ali's answer. 最简单的修复方法是更改​​您的属性名称,如Ali的回答所示。 Just in case you need to keep your property names as is, you can also use a JsonProperty attribute to alter the serialization names (perhaps there's a more generic way using some sort of converter, but didn't think the amount of properties needed it) 如果你需要保持你的属性名称,你也可以使用JsonProperty属性来改变序列化名称(也许有一种更通用的方式使用某种转换器,但不认为需要的属性数量)

    class subjects
    {
        [JsonProperty("subject_id")]
        public int id { get; set; }
        [JsonProperty("subject_name")]
        public string name { get; set; }
        [JsonProperty("subject_class")]
        public int class_name { get; set; }
        [JsonProperty("subject_year")]
        public string year { get; set; }
        [JsonProperty("subject_code")]
        public string code { get; set; }
    }

If you never need the root subjects you can also skip it without dynamic or an extra class with something like: 如果您从不需要根主题,您也可以在没有动态的情况下跳过它,或者额外的课程,例如:

subjects[] arr = JObject.Parse(result)["subjects"].ToObject<subjects[]>();

( JObject is part of the namespace Newtonsoft.Json.Linq ) JObject是名称空间Newtonsoft.Json.Linq一部分)

You need to create a structure like this: 你需要创建一个这样的结构:

public class Subjects
{
    public List<Subject> subjects {get;set;}
}

public class Subject
{
    public string subject_id {get;set;}
    public string subject_name {get;set;}
}

Then you should be able to do: 然后你应该能够做到:

Subjects subjects = JsonConvert.DeserializeObject<Subject>(result);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM