如何从json数组字符串中获取键值对

时间:2019-04-17 18:52:55

标签: c# asp.net json

我调用了某人的Web api方法,该方法返回一个列表:

"[{"departmentNumber":"1","departmentName":"food"},{"departmentNumber":"2","departmentName":"beverage"},{"departmentNumber":"3","departmentName":"apparel"}]"

我的部门班级:

public class Department
{
    private int _departmentID;
    private string _departmentName;

    public Department(int DepartmentID, string DepartmentName)
    {
        this._departmentID = DepartmentID;
        this._departmentName = DepartmentName;
    }

    public int DepartmentID
    {
        get { return _departmentID; }
        set { _departmentID = value; }
    }

    public string DepartmentName
    {
        get { return _departmentName; }
        set { _departmentName = value; }
    }
}

问题是我尝试将json字符串反序列化回列表时,只有DepartmentName数据能够绑定,而DepartmentNumber却没有,因为我的类具有DepartmentID属性。我如何知道无需更改班级即可解决此问题?

2 个答案:

答案 0 :(得分:1)

使用Json.Net

public class Department {
    [JsonProperty("departmentNumber ")]
    public int DepartmentNumber {get;set;}
    [JsonProperty("departmentName ")]
    public string DepartmentName {get;set;}
}

List<Department> departments = JsonConvert.DeserializeObject<List<Department>>(jsonString);

答案 1 :(得分:0)

您可以执行以下操作。

ENTRYPOINT java -jar movie-discovery-server-0.0.1-SNAPSHOT.jar --server.port=$PORT