2012-02-13 15 views
0

JSON.netを使用してSQLデータベースにJSONをロードしようとしています。JSONの問題。デシリアライズしてデータテーブルに変換する

他のJSONレスポンスに問題はありませんでしたが、次の形式は正しくデシリアライズされません。

{"Report":["2012m01d01","2012m01d02","2012w01","2012m01","2012m01d03","2012m01d04","2012m01d05","2012m01d06","2012m01d07","2012m01d08"], 
"Realtime":null} 

誰にも、なぜこれがどういうものかについての洞察を提供できますか?私は次のコードを使用してデシリアライズします。

public void Deserialize(String jsonText, ref DataTable dt) 
     { 
      JsonSerializer json = new JsonSerializer(); 
      json.NullValueHandling = NullValueHandling.Ignore; 
      json.ObjectCreationHandling = ObjectCreationHandling.Replace; 
      json.MissingMemberHandling = MissingMemberHandling.Ignore; 
      json.ReferenceLoopHandling = ReferenceLoopHandling.Serialize; 
      StringReader sr = new StringReader(jsonText); 
      JsonTextReader reader = new JsonTextReader(sr); 
      dt = (DataTable)json.Deserialize(reader, typeof(DataTable)); 

      reader.Close(); 
     } 

これを解決する最良の方法は何ですか?これは他のJSONレスポンスとうまく動作します!

+3

"次の形式は正しくデシリアライズされないようです"というエラーが表示されたり、結果が期待できませんか?どのようなエラーと何が結果ですか?あなたはそれがうまく動作するいくつかの例を与えることができますか? –

+0

.NET 4.0を使用している場合は、こちらを参照してください。http://www.drowningintechnicaldebt.com/ShawnWeisfeld/archive/2010/08/22/using-c-4.0-and- dynamic-to-parse-json.aspx – MethodMan

+0

これは間違いなくエラーを生成しません。それは民主主義化していないようです。しかし、以下のデシリアライズはうまくいきます。 ["" accountID ":1、" profileID ":null、" name ":"ページ "、" ID ":" 18d039ae0360 "、" language ":null、" type ":null、" Category ":null、" IsHierarchy ":false、" IntervalsEnabled ":true、" IsRealtimeCompatible ":true、" properties ":null}、(など) – user1207242

答えて

0

JSONは、単一のDataTableに変換されませんhirarchial情報を、保持することができる、事前に

おかげではなく、データセットとテーブルの関係を使用して、いくつかの中へ。

私はあなたが、これは単一のオブジェクト

であるが、これは適切にデシリアライズしない、これは何の問題

{ 
    "accountID": 1, 
    "profileID": null, 
    "name": "Pages", 
    "ID": "18d039ae0360", 
    "language": n‌​ull, 
    "type": null, 
    "Category": null, 
    "IsHierarchy": false, 
    "IntervalsEnabled": true, 
    "IsRe‌​altimeCompatible": true, 
    "properties": null 
} 

とデシリアライズ言う

http://jsonlint.comバリであなたのJSONを入れて、フォーマッタ。

{ 
    "Report": [ 
     "2012m01d01", 
     "2012m01d02", 
     "2012w01", 
     "2012m01", 
     "2012m01d03", 
     "2012m01d04", 
     "2012m01d05", 
     "2012m01d06", 
     "2012m01d07", 
     "2012m01d08" 
    ], 
    "Realtime": null 
} 

は実際にあなたの問題はないserializion

最初は単一のオブジェクトであるとのDataTable にデシリアライズすることができ、他のレポートオブジェクトのリストへの参照を保持するオブジェクトであるデータの取り扱いです。

ので、手動でこのコードが動作する2つのDataTableに

をあなたのオブジェクトからコンバータを記述する必要があり、私はあなたがあなたの二つのテーブルのための変換関数を作成することができると確信してい

  var settings = new JsonSerializerSettings 
     { 

      MissingMemberHandling = MissingMemberHandling.Error, 
      NullValueHandling = NullValueHandling.Include 

     }; 
     JsonSerializer json = JsonSerializer.Create(settings); 

     json.Error += (x, y) => 
     { 
      var s = y.ErrorContext; 
      var t = y.CurrentObject; 

     }; 

     StringReader sr = new StringReader(jsonString); 
     JsonTextReader reader = new JsonTextReader(sr); 


     var o = json.Deserialize<ClsReport>(reader); 

     string sf = o.ReportItems[2]; 

・ホープ、この役に立たない

関連する問題