私はモバイルアプリケーションで使用する予定の.netを使用して基本的なWebサービスを作成しました。現在のところ、Jsonは出力していますが、必要な構造ではありません。私はJSONを.NETでシリアライズ
[DataContract]
class PoiList
{
[DataMember]
public List<Poi> Pois { get; set; }
}
[DataContract]
class Poi
{
[DataMember]
public string title { get; set; }
[DataMember]
public string latitude { get; set; }
[DataMember]
public string longitude { get; set; }
}
を作成しました
モデルは、私はいくつかのテストデータを追加しました:
PoiList poiList = new PoiList
{
Pois = new List<Poi>()
};
Poi p = new Poi
{
title = "whatever",
latitude = "-2.45554",
longitude = "52.5454645"
};
poiList.Pois.Add(p);
p = new Poi
{
title = "weeee",
latitude = "-2.45554",
longitude = "52.5454645"
};
poiList.Pois.Add(p);
string ans = JsonConvert.SerializeObject(poiList, Formatting.Indented);
これは、返される文字列は次のようになります。
{ "Pois": [ { "title": "shit", "latitude": "-2.45554", "longitude": "52.5454645" }, { "title": "weeee", "latitude": "-2.45554", "longitude": "52.5454645" } ] }
...出力されたJsonを次のようにしたいのです:
string TempString = @"{ ""pois"":
[{ ""poi"":
{
""title"": ""Test title"",
""latitude"": ""-2.4857856"",
""longitude"": ""54.585656""
}},
{ ""poi"":
{
""title"": ""Halfords"",
""latitude"": ""-2.575656"",
""longitude"": ""53.5867856""
}}]}";
唯一の違いは、リスト内の各オブジェクトの隣にある「poi」です。これを含める簡単な方法はありますか?私はnewtonsoft.jsonパッケージを使用しています。
特別な "poi"ラッパーが必要な特別な理由はありますか?次のようにシリアル化する匿名オブジェクトを作成できますか?それは冗長なようだ。 –
私はjsonを初めて使う以外の理由はなく、プロパティの各セットにpoiラッパーを明示的に与えることによって、json文字列を配列に戻す方法を見つけ出すことしかできませんでした。私はもっと良い方法があると確信していますが、今はただのものが必要です。 –