In this method, a JSON String will be returned from GetDataSetColumns() which called API from a Signage System(Xibo) to get dataset. And I want to parse it to JSON Object.
I googled it many times and found some people have same problem too. But it seems their solution are not working for me. Such as parsing to JArray or String.Trim()
public async Task<String> GetColumnIdByName(int dataSetId, String heading)
{
String columns = await GetDataSetColumns(dataSetId);
columns.Replace("[", "");
columns.Replace("]", "");
columns.TrimStart().TrimEnd();
**JObject json = JObject.Parse(columns);**
Debug.WriteLine(json);
foreach (KeyValuePair<string, JToken> pair in json)
{
if (pair.Key.ToString().Equals("dataSetColumnId"))
{
if(pair.Value.ToString().Equals(heading))
{
return pair.Value.ToString();
}
}
}
return null;
}
Here's the JSON returned from GETDataSetColumns method and shown in Debug. And I cannot see there is any mistakes in this JSON String.
[
{
"dataSetColumnId": 8,
"dataSetId": 3,
"heading": "item_name",
"dataTypeId": 1,
"dataSetColumnTypeId": 1,
"listContent": null,
"columnOrder": "1",
"formula": null,
"dataType": "String",
"dataSetColumnType": "Value"
},
{
"dataSetColumnId": 9,
"dataSetId": 3,
"heading": "price",
"dataTypeId": 1,
"dataSetColumnTypeId": 1,
"listContent": null,
"columnOrder": "2",
"formula": null,
"dataType": "String",
"dataSetColumnType": "Value"
},
{
"dataSetColumnId": 12,
"dataSetId": 3,
"heading": "category",
"dataTypeId": 1,
"dataSetColumnTypeId": 1,
"listContent": null,
"columnOrder": "3",
"formula": null,
"dataType": "String",
"dataSetColumnType": "Value"
},
{
"dataSetColumnId": 15,
"dataSetId": 3,
"heading": "status",
"dataTypeId": 1,
"dataSetColumnTypeId": 1,
"listContent": null,
"columnOrder": "7",
"formula": null,
"dataType": "String",
"dataSetColumnType": "Value"
}
]
And I also inspected the value of variable by Debugging mode, is it normal to have '\n' and many blankspaces? Moreover, the String.Replace and String.TrimStart() aren't working.
The exception error happens after JObject.Parse.
Exception thrown: 'Newtonsoft.Json.JsonReaderException' in Newtonsoft.Json.dll
Exception thrown: 'Newtonsoft.Json.JsonReaderException' in mscorlib.dll
An exception of type 'Newtonsoft.Json.JsonReaderException' occurred in mscorlib.dll but was not handled in user code
Error reading JObject from JsonReader. Current JsonReader item is not an object: StartArray. Path '', line 1, position 1.
Is there any problem of my JSON string or the way of parsing? Thanks for any help!