简体   繁体   中英

JObject Parse not able to parse String with JSON type

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.

Debugging Mode

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!

I found the answer from this post

I also found other answers online but all of them are not working. Some of them proposed define a class and deserialize the object as that defined class. But I think it isn't the right way because the structure of JSON could be dynamic.

I transfer it to JArray and retrieve the objects from it as JObject. And it works. I saw these comments and I think the validity of JSON isn't the main point of my problem at all.

 String columns = await GetDataSetColumns(dataSetId);
 JArray jArr = (JArray)JsonConvert.DeserializeObject(columns);

 foreach (JObject item in jArr)
 { 
    if(heading.Equals(item["heading"].ToString()))
       {
                return item["dataSetId"].ToString();
       }

 }

Here's example of the JSON string and I get the value by key

{
    "dataSetColumnId": 12,
    "dataSetId": 3,
    "heading": "category",
    "dataTypeId": 1,
    "dataSetColumnTypeId": 1,
    "listContent": null,
    "columnOrder": "3",
    "formula": null,
    "dataType": "String",
    "dataSetColumnType": "Value"
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM