我有一个自定义集合(实现IList),它有一些自定义属性,如下所示:
class FooCollection : IList<Foo> {
private List<Foo> _foos = new List<Foo>();
public string Bar { get; set; }
//Implement IList, ICollection and IEnumerable members...
}
Run Code Online (Sandbox Code Playgroud)
当我序列化时,我使用以下代码:
JsonSerializerSettings jss = new JsonSerializerSettings() {
TypeNameHandling = TypeNameHandling.Auto
};
string serializedCollection = JsonConvert.SerializeObject( value , jss );
Run Code Online (Sandbox Code Playgroud)
它正确地序列化和反序列化所有收集项目; 但是,FooCollection不考虑课堂上的任何额外属性.
无论如何将它们包含在序列化中?
如何添加JArray到JObject?改变时,我得到一个异常jarrayObj进入JObject.
parameterNames = "Test1,Test2,Test3";
JArray jarrayObj = new JArray();
foreach (string parameterName in parameterNames)
{
jarrayObj.Add(parameterName);
}
JObject ObjDelParams = new JObject();
ObjDelParams["_delete"] = jarrayObj;
JObject UpdateAccProfile = new JObject(
ObjDelParams,
new JProperty("birthday", txtBday),
new JProperty("email", txtemail))
Run Code Online (Sandbox Code Playgroud)
我需要这种形式的输出:
{
"_delete": ["Test1","Test2","Test3"],
"birthday":"2011-05-06",
"email":"dude@test.com"
}
Run Code Online (Sandbox Code Playgroud) 我有一个像这样的JSON:
[{
"agencyId": "myCity",
"road": {
"note": "",
"lat": "45.321",
"lon": "12.21",
"streetCode": "290",
"street": "street1",
"fromNumber": "",
"toNumber": "",
"fromIntersection": "",
"toIntersection": ""
},
"changeTypes": ["PARKING_BLOCK", "ROAD_BLOCK"],
},]
Run Code Online (Sandbox Code Playgroud)
和这样的一个类:
public class AlertRoad : BaseAlert
{
[JsonProperty("agencyId")]
[JsonConverter(typeof(StringEnumConverter))]
public AgencyType AgencyId { get; set; }
[JsonProperty("changeTypes")]
[JsonConverter(typeof(StringEnumConverter))]
public ChangeType[] ChangeTypes { get; set; }
[JsonProperty("road")]
public Road RoadInfo { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
AgencyType是枚举,AgencyId的反序列化和序列化工作.
ChangeType是另一个枚举,但ChangeTypes的反序列化和序列化不起作用.我假设原因是ChangeTypes是一个枚举值数组.
问题是:如何反序列化/序列化ChangeTypes字段,或者通常是枚举值数组?
我尝试定义自己的字段转换器,名为ChangeTypeConverter,并将StrinEnumConverter更改为ChangeTypeConverter for ChangeTypes字段,但在ReadJson函数中,reader的值为null.
public class ChangeTypeConverter : JsonConverter
{
public override void WriteJson(JsonWriter writer, …Run Code Online (Sandbox Code Playgroud) 我有一个.NET应用程序,我想用它来查询Elasticsearch.我成功查询了我的Elasticsearch索引.结果看起来类似于:
{
"took":31,
"timed_out":false,
"_shards": {
"total":91,
"successful":91,
"skipped":0,
"failed":0
},
"hits":{
"total":1,
"max_score":1.0,
"hits":[
{
"_index":"my-index",
"_type":"doc",
"_id":"TrxrZGYQRaDom5XaZp23",
"_score":1.0,
"_source":{
"my_id":"65a107ed-7325-342d-adab-21fec0a97858",
"host":"something",
"zip":"12345"
}
},
]
}
}
Run Code Online (Sandbox Code Playgroud)
现在,这个数据可以通过我从Elasticsearch返回的Body属性StringResponse获得.我想反序列化的实际记录(我不想要或需要的took,timed_out等属性)到一个名为C#对象results.为了做到这一点,我有:
var results = JsonConvert.DeserializeObject<List<Result>>(response.Body);
Run Code Online (Sandbox Code Playgroud)
这个Result类看起来像这样:
public class Result
{
[JsonProperty(PropertyName = "my_id")]
public string Id { get; set; }
[JsonProperty(PropertyName = "host")]
public string Host { get; set; }
[JsonProperty(PropertyName = "zip")]
public string PostalCode { get; set; …Run Code Online (Sandbox Code Playgroud) 我有一个使用WebAPI构建的Web服务,它接受JSON请求并相应地响应.核心架构已构建,但没有任何身份验证/授权.
经过大量的谷歌搜索和示范项目,我不知道从哪里开始.我发现了2008年和2009年的大量资料,但没有为WebAPI /单页应用程序提供大量最新的指南/工作流程.我认为工作流程应该如下:
检查用户是否已登录:如何使用javascript完成此操作?我是否将cookie发送到我的webAPI?如果是这样,我是否将该cookie作为参数发送到请求正文中?
让用户登录/注册:这些数据如何加密/解密?当然我不能通过线路发送密码......这是SSL进来的地方吗?
让他们访问他们有权访问的内容:我想我得到了这个 - 我可以在每个请求的基础上在控制器中进行授权.
任何信息都会很棒.
我正在从一组类中使用Newtonsoft.Json创建一个JSON文件.创建的文件非常大,所以我JsonProperty为属性创建了s来减小大小,JsonIgnore并为某些数据类型添加和自定义格式.
结果是从24MB减少到1MB,这很好; 但是,我希望选择在运行时生成完整版本或简化属性版本.
无论如何,我可以让序列化器可选地使用属性吗?
我需要将json反序列化为以下类.
public class Test
{
public string Property { get; set; }
private Test()
{
//NOTHING TO INITIALIZE
}
public Test(string prop)
{
Property = prop;
}
}
Run Code Online (Sandbox Code Playgroud)
我可以创建一个Test实例
var instance = new Test("Instance");
Run Code Online (Sandbox Code Playgroud)
考虑我的json之类的东西
"{ "Property":"Instance" }"
Run Code Online (Sandbox Code Playgroud)
我如何创建Test类的对象,因为我的默认构造函数是私有的,我得到的对象是Property为NULL
我正在使用Newtonsoft Json解析器.
我有一个包含Json的字符串.它看起来像这样:
"status_code":200,
"status_txt":"OK",
"data":
{
"img_name":"D9Y3z.png",
"img_url":"http:\/\/s1.uploads.im\/D9Y3z.png",
"img_view":"http:\/\/uploads.im\/D9Y3z.png",
"img_width":"167",
"img_height":"288",
"img_attr":"width=\"167\" height=\"288\"",
"img_size":"36.1 KB",
"img_bytes":36981,
"thumb_url":"http:\/\/s1.uploads.im\/t\/D9Y3z.png",
"thumb_width":360,
"thumb_height":360,
"source":"http:\/\/www.google.com\/images\/srpr\/nav_logo66.png",
"resized":"0",
"delete_key":"df149b075ab68c38"
}
Run Code Online (Sandbox Code Playgroud)
我试图抓住"img_url".我安装了Json.NET,我在这里发现了类似的问题..
例如这样的事情:
JObject o = JObject.Parse("{'People':[{'Name':'Jeff'},{'Name':'Joe'}]}");
// get name token of first person and convert to a string
string name = (string)o.SelectToken("People[0].Name");
Run Code Online (Sandbox Code Playgroud)
在我来说,我改变("People[0].Name")到("img_url"),("img_url[0])etc..no运气
这是我现在的代码:
public string tempJson { get; set; }
public ActionResult SaveUploadedFile(string test)
{
using (WebResponse wrs = wrq.GetResponse())
using (Stream stream = wrs.GetResponseStream())
using (StreamReader reader = new StreamReader(stream)) …Run Code Online (Sandbox Code Playgroud) 我有一个具有两个DateTime属性的类.我需要使用不同的格式序列化每个属性.我该怎么做?我试过了:
JsonConvert.SerializeObject(obj, Formatting.None,
new IsoDateTimeConverter {DateTimeFormat = "MM.dd.yyyy"});
Run Code Online (Sandbox Code Playgroud)
此解决方案对我不起作用,因为它将日期格式应用于所有属性.有没有办法用不同的格式序列化每个DateTime属性?也许有一些属性?
我有一个字符串:
[
{
"key": "key1",
"value": "{'Time':'15:18:42','Data':'15:18:42'}",
"duration": 5
},
{
"key": "key1",
"value": "{'Time':'15:18:42','Data':'15:18:42'}",
"duration": 5
}
]
Run Code Online (Sandbox Code Playgroud)
我在模特课上:
public class CPacket
{
public string key { get; set; }
public string value { get; set; }
public int duration { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我使用Json.Net,我想将字符串波纹管转换为Json Oject.
CPacket c = JsonConvert.DeserializeObject<CPacket>(strPostData);
Run Code Online (Sandbox Code Playgroud)
但它错误:
Newtonsoft.Json.dll中发生了'Newtonsoft.Json.JsonSerializationException'类型的异常,但未在用户代码中处理
其他信息:无法将当前JSON数组(例如[1,2,3])反序列化为类型'QuoteAPI.Models .CPacket'因为类型需要JSON对象(例如{"name":"value"})才能正确反序列化.