2013-10-28 20 views
10

JArrayJObjectに追加するには? jarrayObjJObjectに変更すると例外が発生します。JObjectにjarrayオブジェクトを追加する方法

parameterNames = "Test1,Test2,Test3"; 

JArray jarrayObj = new JArray(); 

foreach (string parameterName in parameterNames) 
{ 
    jarrayObj.Add(parameterName); 
} 

JObject ObjDelParams = new JObject(); 
ObjDelParams["_delete"] = jarrayObj; 

JObject UpdateAccProfile = new JObject(
           ObjDelParams, 
           new JProperty("birthday", txtBday), 
           new JProperty("email", txtemail)) 

私はこの形式で出力する必要があります。

{ 
    "_delete": ["Test1","Test2","Test3"], 
    "birthday":"2011-05-06",   
    "email":"[email protected]" 
} 

答えて

14

あなたがそれを掲示として、私はあなたのコードには二つの問題を参照してください。

  1. parameterNamesは、カンマを含む単一の文字列ではなく、文字列の配列である必要があります。
  2. JArrayJObjectに直接追加することはできません。 JPropertyに入れて、JObjectにのを追加する必要があります。これは、あなたが「誕生日」と「メール」のプロパティを行っているのと同じです。

修正されたコード:

string[] parameterNames = new string[] { "Test1", "Test2", "Test3" }; 

JArray jarrayObj = new JArray(); 

foreach (string parameterName in parameterNames) 
{ 
    jarrayObj.Add(parameterName); 
} 

string txtBday = "2011-05-06"; 
string txtemail = "[email protected]"; 

JObject UpdateAccProfile = new JObject(
           new JProperty("_delete", jarrayObj), 
           new JProperty("birthday", txtBday), 
           new JProperty("email", txtemail)); 

Console.WriteLine(UpdateAccProfile.ToString()); 

出力:あなたのコードで例外を取得している場合は、あなたの質問に言えば

{ 
    "_delete": [ 
    "Test1", 
    "Test2", 
    "Test3" 
    ], 
    "birthday": "2011-05-06", 
    "email": "[email protected]" 
} 

また、今後の参考のために、それは便利ですまさに例外が何であるか、私たちは推測する必要はありません。それは私たちがあなたを助けることをより簡単にします。

関連する問題