所以我使用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]
我做错了哪里?
感谢。
P.S。以下是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"]]}}}}
答案 0 :(得分:1)
这是一个列表清单。您目前只对一个级别进行索引编制。您需要深入索引两个级别。
txtOut.Text = rootObject.Results.output1.value.ColumnNames[0].ToString() + " : " +
rootObject.Results.output1.value.Values[0][0].ToString();
答案 1 :(得分:1)
因为元素0是一个列表,您可以使用string.Join
来获取可打印的字符串..
txtOut.Text = rootObject.Results.output1.value.ColumnNames[0].ToString() + " : " +
string.Join(";",rootObject.Results.output1.value.Values[0]);
OUTPUT将是:
Scored Probabilities for Class "BackPocket" : 0.0249993801116943;0.999469399452209;0.00426889630034566;0.00104729761369526;Ear
答案 2 :(得分:0)
尝试将此作为您的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; }
}