在我的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文件,我在上面的示例中通过删除类别旁边的其他对象简化了它。我简化它的原因是因为SO不会让整个JSON内容并且错误消息清楚地表明问题出现在这里:
"category":{"2":{"n":"V\u00e9hicules",
更新:
这里是读取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);
}
}
以下是General Config:
class GeneralConfig
{
public string category { get; set; }
public int city { get; set; }
public string brand { get; set; }
public int model { get; set; }
}
错误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对象(例如{&#34; name&#34;:&#34; value&#34;})或将反序列化类型更改为数组或实现的类型像List这样的集合接口(例如ICollection,IList)可以从JSON数组反序列化。 JsonArrayAttribute也可以添加到类型中以强制它从JSON数组反序列化。 路径&#39;类别&#39;,第1行,第26位。
答案 0 :(得分:1)
嗯,首先,你已经回答了自己的问题。 Xamarin是一个运行时,它是如何或为什么需要“理解”JSON?
问题来自Newtonsoft.Json.JsonReaderException
。如异常消息所示,您具有无效的JSON。
问题绝对是位于第26位的Unicode 符号:V\u00e9hicules
。
这个答案应该有所帮助:Serializing foreign languages using JSON.Net
答案 1 :(得分:1)
您的课程似乎定义不正确。
category
显然不是基于JSON数据的字符串。
我认为您需要定义一个容器对象,例如:
public class GeneralConfig
{
int response;
Dictionary<string,CategoryConfig> category;
}
此外,您应该只拨打JsonConvert.DeserializeObject<GeneralConfig>()
,不应该拨打第二个电话。
答案 2 :(得分:-2)
C#没有以数字开头的属性。例如,"2":
无法反序列化为属性。
此外,你的json在很多地方都有转义字符`\ u00',也许这也是一个问题。