2017-06-05 21 views
0

ここでは、FAQDataをJSON文字列にしたいと考えています。まず、私はこの単純なクラスがあります。新しいリストに新しい項目を追加する

public class FAQData 
{ 
    public string FAQQuestion { get; set; } 
    public string FAQAnswer { get; set; } 
} 

をそして、私はそれを処理するかどうかはわかりませんどこそして、これは

var faqData = JsonConvert.SerializeObject(new List<FAQData> 
    { 
     { 
      FAQQuestion = "Question 1?", 
      FAQAnswer = "This is the answer to Question 1." 
     }, 
     { 
      FAQQuestion = "Question 2?", 
      FAQAnswer = "This is the answer to Question 2." 
     }, 
    }) 

明らかに上記の構文が正しくありません...です。私は遊んでいて、さまざまなGoogle検索を試してみましたが、そこに着くことはできません。 FAQData JSON文字列の結果はこのように見えるようにするために何がしたいことは次のとおりです。

[ 
    {"FAQQuestion": "Question 1?", "FAQAnswer": "This is the answer to Question 1."}, 
    {"FAQQuestion": "Question 2?", "FAQAnswer": "This is the answer to Question 2."} 
] 
+0

あなたは出力が [ { "FAQQuestion": "質問1?"、 "FAQAnswer": "これは1の質問への答えである"}になりたいわけか、 { "FAQQuestion" : "質問2?"、 "FAQAnswer": "質問2の回答です。"} ] – Vinod

+0

はい、正しいです。ありがとうございました! –

答えて

2

あなたはnew FAQData()を忘れてしまった:

JsonConvert.SerializeObject(new List<FAQData> 
{ 
    new FAQData() 
    { 
     FAQQuestion = "Question 1?", 
     FAQAnswer = "This is the answer to Question 1." 
    }, 
    new FAQData() 
    { 
     FAQQuestion = "Question 2?", 
     FAQAnswer = "This is the answer to Question 2." 
    }, 
}); 
+0

ありがとう!今すぐテスト! –

0

あなたがJSONにシリアライズする前に、操作する変数リストを作成することができます。

  var faqList = new List<FAQData> 
      { 
       new FAQData() 
       { 
        FAQQuestion = "Question 1?", 
        FAQAnswer = "This is the answer to Question 1." 
       }, 
       new FAQData() 
       { 
        FAQQuestion = "Question 2?", 
        FAQAnswer = "This is the answer to Question 2." 
       }, 
      }; 
      faqList.Add(new FAQData() 
      { 
       FAQQuestion = "Question 3?", 
       FAQAnswer = "This is the answer to Question 3." 
      }); 
      var faqData = JsonConvert.SerializeObject(faqList); 
関連する問題