C#JSON.NET从JArray读取字符串导致空字符串?

时间:2014-06-23 13:26:16

标签: c# json json.net

我基本上有一个动态JSON对象,它包含一个名为SectionIDs的属性,它是一个字符串的JArray。

"SectionIDs": [
          "974ec4d7-ef2c-49cf-9ae9-4061ea832797",
          "974ec4d7-ef2c-49cf-9ae9-4061ea832797",
          "974ec4d7-ef2c-49cf-9ae9-4061ea832797"
           ]

但是,我似乎无法获得有关此数据的参考。

考虑这个测试代码:

//section list
JArray jsonSectionArray = (JArray)levelObject.SectionIDs;

for (j = 0; j < jsonSectionArray.Count; j++)
{
     Console.WriteLine("APPENDED : " + (string)jsonSectionArray[j]);
     Console.WriteLine("DIRECT CAST: ", (string)jsonSectionArray[j]);

     sectionID = (string)jsonSectionArray[j];

     Console.WriteLine("JSON: ", sectionID);
}

为什么“APPENDED”是唯一的非空白输出?

APPENDED : 974ec4d7-ef2c-49cf-9ae9-4061ea832797
DIRECT CAST: 
JSON: 

最初的问题是我在我的部分循环找到具有相同ID的部分,但是:

private SectionView getSectionByID(string id){
    //always id == " "
}

1 个答案:

答案 0 :(得分:3)

您的Console.WriteLine行有拼写错误:

Console.WriteLine("DIRECT CAST: ", (string)jsonSectionArray[j]);

应该是:

Console.WriteLine("DIRECT CAST: " + (string)jsonSectionArray[j]);
// ------------------------------/\

或:

Console.WriteLine("DIRECT CAST: {0}", (string)jsonSectionArray[j]);