C# 为什么JsonConvert.DeserializeObject<;T>;抛出JsonReaderException?

C# 为什么JsonConvert.DeserializeObject<;T>;抛出JsonReaderException?,c#,json,xamarin,xamarin.forms,C#,Json,Xamarin,Xamarin.forms,在我的Xamarin表单项目中,我解析从文件加载的JSON数据。 无论如何,JSON数据是1000%有效的,因为我可以在其他地方解析它,并且它已经被本地Android应用程序(Java)和PHP使用,没有任何问题,但不知何故Xamarin无法理解它,它给了我以下错误: 未处理的异常: Newtonsoft.Json.JsonReaderException: Unexpected character encountered while parsing value: {. Path 'categor

在我的Xamarin表单项目中,我解析从文件加载的JSON数据。 无论如何,JSON数据是1000%有效的,因为我可以在其他地方解析它,并且它已经被本地Android应用程序(Java)和PHP使用,没有任何问题,但不知何故Xamarin无法理解它,它给了我以下错误:

未处理的异常:

Newtonsoft.Json.JsonReaderException: Unexpected character encountered while parsing value: {. Path 'category', line 1, position 26.
这是我的JSON数据:

{"response":1,"category":{"2":{"n":"V\u00e9hicules","d":"C-Vehicules","i":"icon-car","l":{"3":{"n":"Voitures","d":"Voitures"},"4":{"n":"Motos","d":"Motos"},"6":{"n":"Nautisme","d":"Nautisme"},"7":{"n":"\u00c9quipement Auto","d":"Equipement-Auto"},"8":{"n":"\u00c9quipement Moto","d":"Equipement-Moto"},"10":{"n":"Equipement Nautisme","d":"Equipement-Nautisme"}}},"3":{"n":"Immobilier","d":"C-Immobilier","i":"icon-office","l":{"15":{"n":"Ventes immobili\u00e8res","d":"Ventes-immobilieres"},"16":{"n":"Locations","d":"Locations"},"17":{"n":"Bureaux - Commerces","d":"Bureaux-Commerces"}}},"1":{"n":"Emploi","d":"C-Emploi","i":"icon-bag","l":{"1":{"n":"Offres d'emploi","d":"Offres-d--emploi"}}},"9":{"n":"Services","d":"C-Services","i":"icon-service","l":{"18":{"n":"Prestations de services","d":"Prestations-de-services"},"19":{"n":"Cours particuliers","d":"Cours-particuliers"}}},"4":{"n":"Vacances","d":"C-Vacances","i":"icon-plane","l":{"20":{"n":"Locations de vacances","d":"Locations-de-vacances"},"21":{"n":"Chambres d'h\u00f4tes","d":"Chambres-d--hotes"},"22":{"n":"Campings","d":"Campings"}}},"7":{"n":"Loisirs","d":"C-Loisirs","i":"icon-music","l":{"24":{"n":"CD Musique","d":"CD-Musique"},"23":{"n":"DVD Films","d":"DVD-Films"},"27":{"n":"Jeux - Jouets","d":"Jeux-Jouets"},"25":{"n":"Livres","d":"Livres"},"26":{"n":"Sports","d":"Sports"}}},"5":{"n":"Maison","d":"C-Maison","i":"icon-house","l":{"36":{"n":"Accessoires","d":"Accessoires"},"28":{"n":"Ameublement","d":"Ameublement"},"30":{"n":"Arts de la table","d":"Arts-de-la-table"},"38":{"n":"B\u00e9b\u00e9","d":"Bebe"},"37":{"n":"Bijoux - Montres","d":"Bijoux-Montres"},"33":{"n":"Bricolage","d":"Bricolage"},"35":{"n":"Chaussures","d":"Chaussures"},"31":{"n":"D\u00e9coration","d":"Decoration"},"29":{"n":"Electrom\u00e9nager","d":"Electromenager"},"32":{"n":"Linge de maison","d":"Linge-de-maison"},"34":{"n":"V\u00eatements","d":"Vetements"}}},"6":{"n":"Multim\u00e9dia","d":"C-Multimedia","i":"icon-phone","l":{"40":{"n":"Consoles - Jeux vid\u00e9o","d":"Consoles-Jeux-video"},"41":{"n":"Image - Son","d":"Image-Son"},"39":{"n":"Informatique","d":"Informatique"},"42":{"n":"T\u00e9l\u00e9phonie","d":"Telephonie"}}},"8":{"n":"Mat\u00e9riel professionnel","d":"C-Materiel-professionnel","i":"icon-tool","l":{"47":{"n":"Equipements","d":"Equipements"},"43":{"n":"Mat\u00e9riel agricole","d":"Materiel-agricole"},"45":{"n":"Mat\u00e9riel BTP","d":"Materiel-BTP"},"46":{"n":"Outillage","d":"Outillage"},"48":{"n":"Restauration - caf\u00e9","d":"Restauration-cafe"},"44":{"n":"Transport","d":"Transport"}}}}}
这是发生异常的行:

var general = JsonConvert.DeserializeObject<GeneralConfig>(config);
更新:

下面是读取JSON的代码:

namespace HelloWorldApp
{
class Config
{

    string config = "";

    public Config()
    {
        var assembly = typeof(MainPage).GetTypeInfo().Assembly;
        //Stream stream = assembly.GetManifestResourceStream("HelloWorldApp.Resources.configs.json");
        string[] resources = Assembly.GetExecutingAssembly().GetManifestResourceNames();
        foreach (string resource in resources)
        {
            if (resource.EndsWith(".json"))
            {
                Stream stream = assembly.GetManifestResourceStream(resource);
                if (stream != null)
                {
                    using (var reader = new System.IO.StreamReader(stream))
                    {
                        config = reader.ReadToEnd();
                    }
                }
            }
        }

    }

    public void getCategory(int id)
    {
        var general = JsonConvert.DeserializeObject<GeneralConfig>(config);
        var category = general.category;
        var cats = JsonConvert.DeserializeObject<List<CategoryConfig>>(config);
        foreach (var item in cats)
        {
            Console.WriteLine("Cat: " + item.n);
        }
    }
错误2:

Newtonsoft.Json.JsonSerializationException: Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'HelloWorldApp.CategoryConfig' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.
要修复此错误,请将JSON更改为JSON对象(例如{“name”:“value”}),或将反序列化类型更改为数组或实现可从JSON数组反序列化的集合接口(例如ICollection、IList)类似列表的类型。还可以将JsonArrayAttribute添加到类型中,以强制它从JSON数组反序列化。 路径“类别”,第1行,位置26。

C#没有以数字开头的属性。例如,
“2”:
无法反序列化为属性


此外,json在许多地方都有转义字符“\u00”,这可能也会引起问题。

首先,您已经回答了自己的问题。Xamarin是一个运行时,它需要如何或为什么“理解”JSON

问题来自
Newtonsoft.Json.JsonReaderException
。如异常消息所示,您的JSON无效

问题绝对在于位置26处的Unicode符号:
V\u00e9hicules


这个答案应该有帮助:

您的类似乎定义不正确

category
显然不是基于JSON数据的字符串

我认为您需要定义一个容器对象,例如:

public class GeneralConfig
{
    int response;
    Dictionary<string,CategoryConfig> category;
}
公共类通用配置
{
int响应;
词典类别;
}

另外,您应该只调用
JsonConvert.DeserializeObject()
,不应该进行第二次调用。

我使用:同样的JSON在Java中解析得非常完美,我认为C没有自己的JSON规则,或者是这样吗?@GeraldVersluis奇怪,JSONLint并说它有效。JSON2Charp说noIt可以,没有不同的规则。JSON错误表示字符串有问题。至少,这个字符串包含不应该存在的转义序列。没有理由在Unicode中使用转义序列string@PanagiotisKanavosJSON文本中没有转义序列,\u00e是对法语字母的unicode翻译,它根本不是unicode符号。OP没有发布实际的字符串,这使得解决问题更容易hard@PanagiotisKanavos这是实际数据,只是简化了一点,好吧,我将发布完整的JSON,我想这与错误无关。Unicode字符串没有转义序列。您可以在.NET中输入
:“Véhicules”
string@user9993文本的第26位是
{“2”
因此,我认为您在这里的思路根本不对。Category是一个具有多个子对象的对象,我应该声明它的类型是什么?您需要为dictionary项定义自定义类,然后GeneralConfig将需要包含某种dictionary作为成员。哦,这很有意义,我有一个Category对象,让我来解释一下Ry和返回,1分钟。我认为这是正确的路径,但它给了我这个错误:(见最新的问题)你需要更好地理解数据是如何进入你的应用程序的。如果你不能使它在这一点上工作,我会考虑生产一个MCVE。
Newtonsoft.Json.JsonSerializationException: Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'HelloWorldApp.CategoryConfig' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.
public class GeneralConfig
{
    int response;
    Dictionary<string,CategoryConfig> category;
}