2017-02-21 3 views
1

しますC#は私はこのようなJSONオブジェクトを持ちたい願望JSON形式

では、C#でこのコードを持っている:

var list = new ArrayList(); 
foreach (var item in stats) 
{ 
    list.Add(new { item.date.Date, item.conversions }); 
} 

return JsonConvert.SerializeObject(new { list }); 

は今私のJSONは次のようなものです:

enter image description here

私はこの形式のJsonを持っています:

//{01/21/2017,14} 
//{01/22/2017,17} 
//{01/23/2017,50} 
//{01/24/2017,0} 
//{01/25/2017,2} 
//{01/26/2017,0} 
+2

'{something、something}'は有効なJSON形式ではありません。 JSONは '{key:value}'のペアです。代わりに配列を使ってみてください: '[" 01/21/2017 "、" 14 "]' – Rajesh

+0

各日付の後の "14,17,50,0,2,0"の値はどういう意味ですか? –

+0

@ThiagoCustodioそれは毎日何かの量です – mohammad

答えて

-1
using Newtonsoft.Json; 
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 

namespace TestsJson 
{ 
    class Model 
    { 
     public DateTime Date { get; set; } 

     public int Clicks { get; set; } 

     public Model(DateTime date, int clicks) 
     { 
      Date = date; 
      Clicks = clicks; 
     } 
    } 

    class Program 
    { 
     static void Main(string[] args) 
     { 
      var data = new List<Model>() 
      { 
       new Model(new DateTime(2017, 01, 21), 14), 
       new Model(new DateTime(2017, 01, 22), 17), 
       new Model(new DateTime(2017, 01, 23), 50), 
       new Model(new DateTime(2017, 01, 24), 0), 
       new Model(new DateTime(2017, 01, 25), 2), 
       new Model(new DateTime(2017, 01, 26), 0) 
      }; 

      foreach (var model in data) 
      { 
       var json = "{" + JsonConvert.SerializeObject(model.Date.ToShortDateString()) + ":" + model.Clicks + "}"; 
       Console.WriteLine(json); 
      } 

      Console.Read(); 
     } 
    } 
} 
+0

あなたのコードを説明するのはどうですか? –

+0

それはかなり自明です。 – Viezevingertjes

1

JSONオブジェクトとして文字列を作成できます。例:

var list = new List<string>(); 
foreach (var item in stats) 
    { 
     list.Add(String.Format("{0},{1}",item.date.Date, item.conversions)); 
    } 

return JsonConvert.SerializeObject(new { list }); 

//I haven't tested the code. 
関連する問題