我有一个基本上看起来像这样的JSON:
{
"body":
{
"mode": "raw",
"raw": "{
\"Token\" : \"123123\", \"queryName\" : \"testMethod\" ,\"dataTestToSEND\" :{\"IDs\":[\"B00448MZUW\",\"B071F7LBN6\",\"B01BBZJZHQ\"],\"Marketplace\":\"southAmerica\",\"Region\":\"West\",\"PricingMethod\":0}} "a
},
"description": "some description here"
}
}
当我将它转换为C#对象类时,我得到了这个:
public class Body
{
public string mode { get; set; }
public string raw { get; set; }
}
public class RootObject
{
public Body body { get; set; }
public string description { get; set; }
}
我在这里使用了json2csharp工具..
现在让我感到困惑的是你可以看到的“原始”属性..这个工具把它转换成了一个字符串,但这对我来说真的不像是一个字符串?
相反,原始的,我看到它的方式,应该是一个包含这样的东西的类:
public class Raw
{
public string Token { get; set; }
public string queryName { get; set; }
public List<string//?not sure which type does this needs to be set to?>
dataTestToSEND { get; set }
public string marketplace { get; set; }
public string Region { get; set }
}
有人可以帮我解决这个问题吗?如何为此JSON构建一组适当的类和对象?这对我来说非常困惑......
答案 0 :(得分:3)
json2csharp将您的原始属性转换为字符串,因为它无法使用转义字符正确解析JSON文档。 删除转义字符,以便让json2csharp创建正确的尖锐类。
{"body":{"mode":"raw","raw":{"Token":"123123","queryName":"testMethod","dataTestToSEND":{"IDs":["B00448MZUW","B071F7LBN6","B01BBZJZHQ"],"Marketplace":"southAmerica","Region":"West","PricingMethod":0}}},"description":"somedescriptionhere"}
答案 1 :(得分:3)
您可以使用JSON.NET将您的json转换为特定类
官方网站:http://www.newtonsoft.com/json
你可以从json中删除反斜杠,让JObject解释它。
public class Raw
{
public Raw(string json)
{
JObject jObject = JObject.Parse(json);
JToken jRaw = jObject["raw"];
Token = (string) jRaw["token"];
queryName = (string) jRaw["queryName"];
dataTestToSEND = (List<string>) jRaw["dataTestToSEND"];
marketplace = (string) jRaw["Marketplace"]
Region= jRaw["players"].ToArray();
}
public string Token {get;set;}
public string queryName {get;set;}
public List<string> dataTestToSEND {get;set}
public string marketplace {get;set;}
public string Region{get;set}
}
// Call User with your json
string json = @"{""body"":{""mode"":""raw"",""raw"":{""Token"":""123123"",""queryName"":""testMethod"",""dataTestToSEND"":{""IDs"":[""B00448MZUW"",""B071F7LBN6"",""B01BBZJZHQ""],""Marketplace"":""southAmerica"",""Region"":""West"",""PricingMethod"":""0""}}},""description"":""somedescriptionhere""}";
Raw raw = new Raw(json);
答案 2 :(得分:2)
构建自定义转换器以将字符串属性转换为类型
public class RawConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return true;
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
if (reader.TokenType != JsonToken.String)
{
throw new InvalidOperationException();
}
var value = (string)reader.Value;
var obj = JsonConvert.DeserializeObject(value, objectType);
return obj;
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
var str = JsonConvert.SerializeObject(value);
writer.WriteValue(str);
}
}
并在您需要转换器的属性上放置一个属性
public class Body
{
public string mode { get; set; }
// This property is a raw string in the json document
[JsonConverter(typeof(RawConverter))]
public Data raw { get; set; }
}
public class RootObject
{
public Body body { get; set; }
public string description { get; set; }
}
public class Data
{
public string Token { get; set; }
public string queryName { get; set; }
public DataTestToSEND dataTestToSEND { get; set; }
}
public class DataTestToSEND
{
public string[] IDs { get; set; }
public string Marketplace { get; set; }
public string Region { get; set; }
public int PricingMethod { get; set; }
}
现在你可以反序列化给定的json
{
"body": {
"mode": "raw",
"raw": "{\"Token\":\"123123\",\"queryName\":\"testMethod\",\"dataTestToSEND\":{\"IDs\":[\"B00448MZUW\",\"B071F7LBN6\",\"B01BBZJZHQ\"],\"Marketplace\":\"southAmerica\",\"Region\":\"West\",\"PricingMethod\":0}}"
},
"description": "some description here"
}
与
var result = JsonConvert.DeserializeObject<RootObject>( jsonString );
使用反序列化和序列化
在.net fiddle上的实例