JSONがファイルに保存されているので、test.jsonとしましょう。私はファイルを開き、その中のJSONを文字列で読み込み、それをオブジェクトに逆シリアル化しようとしています。ファイルが正しく開かれ、文字列が読み込まれます(しかし、エスケープは文字列で表示されます)。逆直列化を試みると、オブジェクトのすべてのフィールドにnullが返されます。ここでC#Json Deserializeがnullを返す
はJSONをファイルに保存する方法である:ここではhttp://pastebin.com/Q6hdiJAD
はJSONがファイルから読み込まれた後、私の文字列で保存されている方法である:ここではhttp://pastebin.com/HaB480Ww
はJSONをデシリアライズする必要があり、コードである(私は)複数のファイルから複数のjsonsを取り、それらをデシリアライズし、リストに追加:
List<Root> raw = new List<Root>();
string[] files = Directory.GetFiles(HostingEnvironment.MapPath("~/Content/Files"));
foreach (string path in files)
{
using (StreamReader sr = new StreamReader(path))
{
string json = sr.ReadToEnd();
Root root = JsonConvert.DeserializeObject<Root>(json);
raw.Add(root);
}
}
そしてここでは、私のクラスである:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using APIGovApp.Models;
namespace APIGovApp.Classes
{
public class XmlModel
{
public string Staff { get; set; }
public List<Nomenclator> noms { get; set; }
}
public class Root
{
public XmlModel nom_localitati { get; set; }
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace APIGovApp.Models
{
public class Nomenclator
{
public int cod_jud { get; set; }
public int cod { get; set; }
public int cod_pol { get; set; }
public string denumire { get; set; }
public string cod_tpl { get; set; }
public int cod_postal { get; set; }
public string cod_sar { get; set; }
public int cod_loc_jud { get; set; }
public int loc_cod { get; set; }
public string are_primarie { get; set; }
public int cod_fiscal_primarie { get; set; }
public int cod_politie_tata { get; set; }
public string sar_cod_mf { get; set; }
public int cod_siruta { get; set; }
public int cod_siruta_tata { get; set; }
}
}
私はコンテストに参加しており、できるだけ早くこれを必要としています。助けてください。
「いいえ'JUD_COD'の代わりに' cod_jud'のようにJSONにマッチしません。 JSONをhttp://json2csharp.com/にアップロードして、正しいクラスセットを自動生成します。 – dbc