我有一个原始字符串.我只是想验证字符串是否是有效的JSON.我正在使用JSON.NET.
{
"title":"Mozilla Firefox",
"id":24,
"parent":2,
"dateAdded":1356753810000000,
"lastModified":1356753810000000,
"type":"text/x-moz-place-container",
"children":[]
}
Run Code Online (Sandbox Code Playgroud)
class Bookmark
{
public string title;
public string id;
[JsonProperty(ItemConverterType = typeof(JavaScriptDateTimeConverter))]
public DateTime dateAdded;
[JsonProperty(ItemConverterType = typeof(JavaScriptDateTimeConverter))]
public DateTime lastModified;
public string type;
public string root;
public long parent;
public List<Bookmark> children;
}
private static void Main(string[] args)
{
var json = File.ReadAllText(@"T:/bookmarks-2013-11-13.json");
var bookmarks = JsonConvert.DeserializeObject<Bookmark>(json);
}
Run Code Online (Sandbox Code Playgroud)
我尝试运行时遇到异常,
附加信息:阅读日期时出错.意外的令牌:整数.路径'dateAdded'
我想通过使用JavaScriptDateTimeConverter,JSON.NET可以弄清楚如何反序列化那些unix时间戳(女士从epoch开始的μs).最简单的方法是什么?
无法在转换器上查找文档......如果有必要,自己编写文档可能不会太难.
编辑:这些实际上是微秒,而不是毫秒.
我试图使用Commandline将json字符串传递给C#-Program.
JSON-String看起来像这样:
{
"config": {
"script": {
"script_name": "test",
"dir": "D:\\test",
"destination": "M:\\neu\\test",
"params": "/b /s /r:3 /w:5"
}
}
}
Run Code Online (Sandbox Code Playgroud)
在Commandline中它看起来像这样:
{"config":{"script":{"script_name":"test","dir":"D:\\test","destination":"M:\\neu\\test","params":"/b /s /r:3 /w:5"}}}
Run Code Online (Sandbox Code Playgroud)
但是,如果我只是传递字符串,那么它会被分成几块.但我希望我的程序将其视为一个字符串.
我是否必须调整我的JSON-String?
我正在尝试使用他们的 nuget 包从 Youtube 上的一些视频下载字幕。这是一些代码:
var request = _youtube.Search.List("snippet,id");
request.Q = "Bill Gates";
request.MaxResults = 50;
request.Type = "video";
var results = request.Execute();
foreach (var result in results.Items)
{
var captionListRequest = _youtube.Captions.List("id,snippet", result.Id.VideoId);
var captionListResponse = captionListRequest.Execute();
var russianCaptions =
captionListResponse.Items.FirstOrDefault(c => c.Snippet.Language.ToLower() == "ru");
if (russianCaptions != null)
{
var downloadRequest = _youtube.Captions.Download(russianCaptions.Id);
downloadRequest.Tfmt = CaptionsResource.DownloadRequest.TfmtEnum.Srt;
var ms = new MemoryStream();
downloadRequest.Download(ms);
}
}
Run Code Online (Sandbox Code Playgroud)
当 Download 方法被调用时,我收到一个奇怪的 Newtonsoft.JSON 异常,它说:
Newtonsoft.Json.JsonReaderException: 'Unexpected character encountered while parsing value: T. Path …Run Code Online (Sandbox Code Playgroud)