我有一个测试类,我试图序列化:
public class Testing
{
private string _name;
private string _firstname = "firstname";
public string _lastname;
private DateTime _datenow = DateTime.Now;
public DateTime _birthdate = DateTime.Now;
public string Name { get { return _name; } set { _name = value; } }
}
Run Code Online (Sandbox Code Playgroud)
我使用自定义JsonConverter来处理测试类的序列化:
public class TestingConverter : JsonConverter
{
private Type[] _types = new Type[] { typeof(Testing)};
/// <summary>
/// Writes the JSON representation of the object.
/// </summary>
/// <param name="writer">The <see cref="JsonWriter"/> to write to.</param> …Run Code Online (Sandbox Code Playgroud) 假设我想将一组Json数据反序列化为Person对象.
class Person
{
[DataMember]
string name;
[DataMember]
int age;
[DataMember]
int height;
object unused;
}
Run Code Online (Sandbox Code Playgroud)
但是,如果我有如下所示的Json数据:
{
"name":"Chris",
"age":100,
"birthplace":"UK",
"height":170,
"birthdate":"08/08/1913",
}
Run Code Online (Sandbox Code Playgroud)
"birthdate"和"birthplace"字段不属于Person类.但是我仍然希望保留这些字段,那么是否可以使用Json.net或其他可以将这些额外字段存储到Person字段之一的库,如上面声明的"未使用"?
我如何轻松地将此JSON反序列化为OrderDto C#类?有某种方式可以通过属性来做到这一点吗?
JSON:
{
"ExternalId": "123",
"Customer": {
"Name": "John Smith"
}
...
}
Run Code Online (Sandbox Code Playgroud)
C#:
public class OrderDto
{
public string ExternalId { get; set; }
public string CustomerName { get; set; }
...
}
Run Code Online (Sandbox Code Playgroud)
我尝试使用JsonProperty属性,但无法使其正常工作。我的想法是写一个像这样的注释:
[JsonProperty("Customer/Name")]
public string CustomerName { get; set; }
Run Code Online (Sandbox Code Playgroud)
但这似乎不起作用。有任何想法吗?谢谢!:)
我正在从返回JSON响应的Web服务获取数据。这是我的代码:
WebClient client = new WebClient();
var result = client.DownloadString("http://some url");
JObject obj = JObject.Parse(result);
// Location l = new Location();
// l.city = obj["ad"][2]; error here
Run Code Online (Sandbox Code Playgroud)
在这一点上,它返回一个结果,但我收到一个错误:
无法将类型'Newtonsoft.Json.Linq.JToken'隐式转换为'string'
我需要一些帮助,以将返回的数据放入模型中的变量中。
这是我的JSON:
{
data: [
{
address_obj: {
street1: "9518 Front Beach Road",
street2: "",
city: "Panama City Beach",
state: "Florida",
country: "United States",
postalcode: "32407",
address_string: "9518 Front Beach Road, Panama City Beach, FL 32407"
},
Run Code Online (Sandbox Code Playgroud) 我有一个Excel文件,我使用DevExpress编辑,我正在使用NPOI阅读.当我尝试将日期单元格的值作为字符串时,它不保留原始值.
例如:在DevExpress网格中,我设置了这个值:2016-08-12.我想在我的字符串中获得相同的值,但我得到了42689.
获取单元格值的代码如下:
ICell cell = row.GetCell(i);
cell.SetCellType(CellType.String);
string fieldString = cell.StringCellValue;
result = result + ";" + FieldValue;
Run Code Online (Sandbox Code Playgroud)
如何获取原始格式化的日期值?
许多网站(例如twitter,stackexchange)基于HTTP协议提供RESTful OPEN API.我可以基于其他协议(例如原始TCP)设计RESTful服务吗?
我有一个JObject等于:
"Info":
{
"View":"A",
"Product":"B",
"Offer":"Offer1",
"Demo":"body {background-color:red;} #box {border:dotted 50px red;}",
"Log":false
}
Run Code Online (Sandbox Code Playgroud)
如何返回对象的名称"Info"?
我目前正在使用这样的Path属性:
jObject.Name = jObject.Path.Substring(jObject.Path.jObject('.') + 1);
Run Code Online (Sandbox Code Playgroud)
有一个更好的方法吗?
我有一个三维结构......实际上是一个双向链表,有六个节点,即左、右、上、下、进、出。如果一个节点在另一个节点的右侧,那么该节点将明显位于第一个节点的左侧。喜欢

实际上这是一个 3D 结构,但为了便于理解,我给出了一个 2D 示例。现在我必须将其转换为 JSON 格式,以便通过 WCF 将此数据发送到客户端,但由于它包含循环,因此无法将其转换为 JSON。我有这些问题
我正在使用Json.Net来处理 JSON。
我的班级是
public class Node
{
public Document document = null;
public Node left = null;
public Node right = null;
public Node up = null;
public Node down = null;
public Node inside = null;
public Node outside = null;
}
Run Code Online (Sandbox Code Playgroud) npm WARN unmet dependency C:\MyWebProject\node_modules\karma\node_modules\socket.io\node_modules\socket.io-client\node_modules\active-x-obfuscator requires zeparser@'0.0.5' but will load
npm WARN unmet dependency C:\MyWebProject\node_modules\zeparser,
npm WARN unmet dependency which is version 0.0.7
Loading "grunt-karma.js" tasks...ERROR
EXEC : >> error : Cannot find module './ZeParser'
EXEC : warning : Task "karma:unit" not found. Use --force to continue.
Run Code Online (Sandbox Code Playgroud)
我正在尝试安装,npm install zeparser但我仍然得到上述警告.
请帮助.
以下代码将引发异常:
class SimpleClassWithRegex
{
public Regex RegProp { get; set; }
}
[TestMethod]
public void RegexTest()
{
string json = JsonConvert.SerializeObject(new SimpleClassWithRegex {RegProp = null});
// json = {"RegProp":null}
SimpleClassWithRegex obj = JsonConvert.DeserializeObject<SimpleClassWithRegex>(json);
//Above line throws a JsonSerializationException
}
Run Code Online (Sandbox Code Playgroud)
这对我来说似乎很奇怪,有人可以解释为什么这不是一个bug吗?或者也许建议一个解决方法?实例化一个Regex对象代替null当然会阻止它抛出异常.
产生的例外是:
Newtonsoft.Json.JsonSerializationException: Unexpected token when reading Regex. Path 'RegProp', line 1, position 15.
c# ×7
json.net ×7
json ×3
.net ×1
c#-4.0 ×1
devexpress ×1
http ×1
javascript ×1
karma-runner ×1
linq ×1
node.js ×1
npm ×1
npoi ×1
rest ×1