Clark
Clark

Reputation: 2678

C# JSON.NET reading string from JArray results in Blank string?

I basically have a dynamic JSON object which contains a property called SectionIDs which is a JArray of strings.

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

However, I cannot seem to get a reference on this data.

Consider this test code:

//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);
}

Why is "APPENDED" the only non blank output?

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

The original problem was that I was looping through my sections to find the section with the same ID, but:

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

Upvotes: 0

Views: 157

Answers (1)

Andrew Whitaker
Andrew Whitaker

Reputation: 126072

Your Console.WriteLine line has a typo:

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

should be:

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

or:

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

Upvotes: 3

Related Questions