2017-01-16 3 views
1

オブジェクトKeyValuePairをシリアル化できません。たとえば :KeyValuePairがJsonFxシリアル化で動作しない

int[] test = { 0, 1, 2, 3, 4, 5 }; 
List<KeyValuePair<string, object>> test_d = new List<KeyValuePair<string,object>>(); 
test_d.Add(new KeyValuePair<string, object>("temp", 12)); 
test_d.Add(new KeyValuePair<string, object>("temp2", test)); 
Console.WriteLine(JsonFx.JsonWriter.Serialize(test_d)); 

戻る[{},{}] 私はこの不幸な状況をどのように修正すればよいですか?結果が与えなかった "タプル"の意味の中で同様のものを使用する。

+0

JsonFXの代わりにNewtonsoft.Jsonを使用できますか? –

+1

JsonFxは、4年以上サポートされていない6歳のプロジェクトです。 M.Wiśnickiのコードを使用し、答えとしてマークします。 –

答えて

1

サンプルはコンパイルバーではありません。これを試してみる:

{ "TEMP":12、 "TEMP2":[0,1,2,3,4,5]}

var writer = new JsonWriter(); 
Console.WriteLine(writer.Write(test_d)); 

Iが出力有します

1

私は答えはJsonFXについてですけど、あなたも好きJson.NETを使用して、それをシリアル化することができます

int[] test = {0, 1, 2, 3, 4, 5}; 
List<KeyValuePair<string, object>> test_d = new List<KeyValuePair<string, object>>(); 
test_d.Add(new KeyValuePair<string, object>("temp", 12)); 
test_d.Add(new KeyValuePair<string, object>("temp2", test)); 


var x = JsonConvert.SerializeObject(test_d, Formatting.Indented, new JsonSerializerSettings 
{ 
     TypeNameAssemblyFormat = FormatterAssemblyStyle.Simple 
}); 

結果は次のようになります。

[ 
    { 
    "Key": "temp", 
    "Value": 12 
    }, 
    { 
    "Key": "temp2", 
    "Value": [ 
     0, 
     1, 
     2, 
     3, 
     4, 
     5 
    ] 
    } 
] 
関連する問題