JSON解析值未显示



所以我使用JSON.net来解析我的JSON,但是我得到了List of List 对象的错误输出。下面是我的JSON.net代码:

var reader = new StreamReader(GenerateStreamFromString(decodedString));
            var rootObject = JsonConvert.DeserializeObject<RootObject>(reader.ReadToEnd());
txtOut.Text = rootObject.Results.output1.value.ColumnNames[0].ToString() + " : " + 
rootObject.Results.output1.value.Values[0].ToString();

下面是JSON类:

public class Value
{
    public List<string> ColumnNames { get; set; }
    public List<string> ColumnTypes { get; set; }
    public List<List<string>> Values { get; set; }
}
public class Output1
{
    public string type { get; set; }
    public Value value { get; set; }
}
public class Results
{
    public Output1 output1 { get; set; }
}
public class RootObject
{
    public Results Results { get; set; }
}

输出应该是:

Scored Probabilities for Class "BackPocket" : 0.37687685364

而我得到:

Scored Probabilities for Class "BackPocket" : System.Collections.Generic.List`1[System.String]

我哪里做错了?

谢谢。

注:下面是一个JSON示例:

{"Results":{"output1":{"type":"table","value":{"ColumnNames":["Scored Probabilities for Class "BackPocket"","Scored Probabilities for Class "Ear"","Scored Probabilities for Class "Handbag"","Scored Probabilities for Class "SidePocket"","Scored Labels"],"ColumnTypes":["Double","Double","Double","Double","String"],"Values":[["0.0249993801116943","0.999469399452209","0.00426889630034566","0.00104729761369526","Ear"]]}}}}

这是一个列表的列表。你目前只索引了一层深度。你需要索引两层深。

txtOut.Text = rootObject.Results.output1.value.ColumnNames[0].ToString() + " : " + 
rootObject.Results.output1.value.Values[0][0].ToString();

因为元素0是一个列表,你可以使用string.Join来获得一个可打印的字符串。

txtOut.Text = rootObject.Results.output1.value.ColumnNames[0].ToString() + " : " +
                string.Join(";",rootObject.Results.output1.value.Values[0]);

输出将是:

Scored Probabilities for Class "BackPocket" : 0.0249993801116943;0.999469399452209;0.00426889630034566;0.00104729761369526;Ear

试试这个作为你的Json class/Classes

[DataContract]    
public class Value
{
[DataMember]
public List<string> ColumnNames { get; set; }
[DataMember]
public List<string> ColumnTypes { get; set; }
[DataMember]
public List<List<string>> Values { get; set; }
}
[DataContract]
public class Output1
{
[DataMember]
public string type { get; set; }
[DataMember]
public Value value { get; set; }
}
[DataContract]
public class Results
{
[DataMember]
public Output1 output1 { get; set; }
}
[DataContract]
public class RootObject
{
[DataMember]
public Results Results { get; set; }
}

相关内容

  • 没有找到相关文章

最新更新