简体   繁体   English

JObject Parse无法解析JSON类型的字符串

[英]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. 在此方法中,将从GetDataSetColumns()返回一个JSON字符串,该字符串从标牌系统(Xibo)调用API以获取数据集。 And I want to parse it to JSON Object. 我想将其解析为JSON对象。

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() 例如解析为JArray或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. 这是从GETDataSetColumns方法返回并在Debug中显示的JSON。 And I cannot see there is any mistakes in this JSON String. 而且我看不到此JSON字符串有任何错误。

    [
    {
        "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? 而且我还通过“调试”模式检查了变量的值,具有“ \\ n”和许多空格是否正常? Moreover, the String.Replace and String.TrimStart() aren't working. 此外,String.Replace和String.TrimStart()无法正常工作。

Debugging Mode 调试模式

The exception error happens after JObject.Parse. 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? 我的JSON字符串或解析方式有问题吗? 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. 但是我认为这不是正确的方法,因为JSON的结构可能是动态的。

I transfer it to JArray and retrieve the objects from it as JObject. 我将其传输到JArray并从中检索对象作为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. 我看到了这些注释,并且我认为JSON的有效性根本不是我的问题的重点。

 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 这是JSON字符串的示例,我通过键获取值

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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