无法隐式转换类型字符串字符串[]

时间:2016-06-03 10:40:14

标签: c#

我想将字符串存储到字符串数组中,但它显示错误。 这是我的代码:

List <ResponseObject> myresponse =JsonConvert.DeserializeObject<List<ResponseObject>>(responseFromServer);
string [] DomainArray;
for (int i = 0; i < myresponse.Count; i++)
{
    for (int j = 0; j < myresponse[i].EmailAddressSuffixes.Count; j++)
    {
        DomainArray = myresponse[i].EmailAddressSuffixes[j];
    }
}

4 个答案:

答案 0 :(得分:1)

您正尝试将DomainArray(字符串数组)分配给单个字符串。

试试这个,它会将所有值添加到列表中,然后将列表转换为数组:

    List<ResponseObject> myresponse = JsonConvert.DeserializeObject<List<ResponseObject>>(responseFromServer);
    List<string> DomainList = new List<string>();
    for (int i = 0; i < myresponse.Count; i++)
    {
        for (int j = 0; j < myresponse[i].EmailAddressSuffixes.Count; j++)
        {
            DomainList.Add(myresponse[i].EmailAddressSuffixes[j]);
        }
    }
    var DomainArray = DomainList.ToArray();

答案 1 :(得分:0)

List <ResponseObject> myresponse =JsonConvert.DeserializeObject<List<ResponseObject>>(responseFromServer);
var DomainArray = new List<string>();
for (int i = 0; i < myresponse.Count; i++)
{
for (int j = 0; j < myresponse[i].EmailAddressSuffixes.Count; j++)
{
DomainArray.Add( myresponse[i].EmailAddressSuffixes[j] );
}
}

然后您可以使用DomainArray.ToArray()

获取数组(如果需要)

答案 2 :(得分:0)

我在上面的代码中做了两处更改。首先来自@Hugo,第二个来自@Roma

List <ResponseObject> myresponse = JsonConvert.DeserializeObject<List<ResponseObject>>(responseFromServer);
List<string> DomainArray = new List<string>();
for (int i = 0; i < myresponse.Count; i++)
{
    for (int j = 0; j < myresponse[i].EmailAddressSuffixes.Count; j++)
    {
        DomainArray.Add(myresponse[i].EmailAddressSuffixes[j]);
    }
}

答案 3 :(得分:0)

由于数组不是动态的,请考虑使用列表:

List <ResponseObject> myresponse =JsonConvert.DeserializeObject<List<ResponseObject>>(responseFromServer);
List<string> DomainArray = new List<string>();
for (int i = 0; i < myresponse.Count; i++)
{
    for (int j = 0; j < myresponse[i].EmailAddressSuffixes.Count; j++)
    {
        DomainArray.add(myresponse[i].EmailAddressSuffixes[j]);
    }
}