サーバーが欲しいjson形式を作ってみる
下のツールに張る
たとえば↓のようなjsonを
{
"err":"2",
"Ranking":
[
{
"number": "1",
"shopId": "2006",
"shopName":"川崎店",
"totalMedal": "500",
"homeFlg": "0"
},
{
"number": "2",
"shopId": "2006",
"shopName":"都築店",
"totalMedal": "500",
"homeFlg": "1"
}
],
"message":
[
"レイドボス襲来!",
"生誕祭"
]
}
こんな感じである程度自動生成してくれます。余計なソースを切ったりいろいろ設定できます
次にget set とかの自動プロパティがあるとUnityでpublicに読めなくなるので
置換して消してく
jsonでclass変数を使いたい場合、上に[Serializable]を書く
あと数字はなぜか全部longになるのでintなりに直してく
APIDEMO.cs
// <auto-generated />
//
// To parse this JSON data, add NuGet 'Newtonsoft.Json' then do:
//
// using EndAPIValue;
//
// var getPlanetGroupRanking = GetPlanetGroupRanking.FromJson(jsonString);
namespace EndAPIValue
using System;
using System.Collections.Generic;
using System.Globalization;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
public partial class GetPlanetGroupRanking
{
[JsonProperty("err")]
[JsonConverter(typeof(ParseStringConverter))]
public int Err ;
[JsonProperty("Ranking")]
public List<Ranking> Ranking ;
[JsonProperty("message")]
public List<string> Message ;
}
[Serializable]
public partial class Ranking
{
[JsonProperty("number")]
[JsonConverter(typeof(ParseStringConverter))]
public int Number ;
[JsonProperty("shopId")]
[JsonConverter(typeof(ParseStringConverter))]
public int ShopId ;
[JsonProperty("shopName")]
public string ShopName ;
[JsonProperty("totalMedal")]
[JsonConverter(typeof(ParseStringConverter))]
public int TotalMedal ;
[JsonProperty("homeFlg")]
[JsonConverter(typeof(ParseStringConverter))]
public int HomeFlg ;
}
public partial class GetPlanetGroupRanking
{
public static GetPlanetGroupRanking FromJson(string json) => JsonConvert.DeserializeObject<GetPlanetGroupRanking>(json, EndAPIValue.Converter.Settings);
}
public static class Serialize
{
public static string ToJson(this GetPlanetGroupRanking self) => JsonConvert.SerializeObject(self, EndAPIValue.Converter.Settings);
}
internal static class Converter
{
public static readonly JsonSerializerSettings Settings = new JsonSerializerSettings
{
MetadataPropertyHandling = MetadataPropertyHandling.Ignore,
DateParseHandling = DateParseHandling.None,
Converters =
{
new IsoDateTimeConverter { DateTimeStyles = DateTimeStyles.AssumeUniversal }
},
};
}
internal class ParseStringConverter : JsonConverter
{
public override bool CanConvert(Type t) => t == typeof(int) || t == typeof(int?);
public override object ReadJson(JsonReader reader, Type t, object existingValue, JsonSerializer serializer)
{
if (reader.TokenType == JsonToken.Null) return null;
var value = serializer.Deserialize<string>(reader);
int l;
throw new Exception("Cannot unmarshal type int");
}
public override void WriteJson(JsonWriter writer, object untypedValue, JsonSerializer serializer)
{
if (untypedValue == null)
{
serializer.Serialize(writer, null);
return;
}
var value = (int)untypedValue;
serializer.Serialize(writer, value.ToString());
return;
}
public static readonly ParseStringConverter Singleton = new ParseStringConverter();
}
}
[JsonProperty("shopId")]
こういうの使いたかったら、
http://www.newtonsoft.com/jsonへ行って、zipをダウンロードする。
zipファイルを解凍して「Bin\Net20\Newtonsoft.json.dll」をUnityのPluginsフォルダにコピペする。