更新经过一些研究,我发现Visual Studio 2015和Text Visualizer存在问题,您可以看到here。
要重现它,请打开QuickWatch(Shift + F9),然后在搜索框中输入new string(' ', 32769)
。之后点击放大镜玻璃,你应该在弦的中间看到一个“......”......
所以,改变我的问题,是否有办法解决这个问题并使其不截断,所以我可以在没有解决方法的情况下进行复制?
我有这段代码:
JArray bundlesJson = (JArray)JObject.Parse(System.IO.File.ReadAllText(Server.MapPath("~/itens.json")))["bundles"]; // file "itens.json" can be found at http://pastebin.com/7K15yAVd
System.Text.RegularExpressions.Regex rgx = new System.Text.RegularExpressions.Regex(@"(?<sNome>.*?)\/(?<sClasse>.*?)\/(?<sDescricao>.*?)(?:\/(?<sExtras>.*?)\n|\n|$)");
var text = System.IO.File.ReadAllText(Server.MapPath("~/TextFile1.txt")); // TextFile1.txt is too big to put here, so i've uploaded it here: http://pastebin.com/AtxbYPXc
var matches = rgx.Matches(text);
var lstItens = new List<Item>();
var oJson = new JArray();
foreach (System.Text.RegularExpressions.Match match in matches)
{
var item = new Item()
{
sClasse = match.Groups["sClasse"].Value.Trim(),
sDescricao = match.Groups["sDescricao"].Value.Trim(),
sNome = match.Groups["sNome"].Value.Trim(),
sExtras = match.Groups["sExtras"].Value.Trim(),
};
item.PreencherListaBundles(bundlesJson.ToString());
lstItens.Add(item);
}
var result = JsonConvert.SerializeObject(lstItens, Formatting.Indented);
var backResult = JsonConvert.DeserializeObject<List<Item>>(result);
lstItens
列表正确包含所有项目(501项),但result
字符串在搜索"Nome":
时仅返回48个对象,这是必填字段。为什么会这样?
要举例说明错误,请在lstItens[166]
var中查找项目result
,如果您搜索"Nome": "Fiddlehead Fern"
,则可以看到该项目不存在... < / p>
奇怪的是,backResult.Count
将显示501个结果,并且似乎包含所有项目,但是使用必填字段“Nome”在result
var中生成的json中的简单搜索将会产生在48个结果中,如图所示:
Item.cs:
public class Item
{
[JsonProperty(PropertyName = "Nome")]
public string sNome { get; set; }
[JsonProperty(PropertyName = "Classe")]
public string sClasse { get; set; }
[JsonProperty(PropertyName = "Descricao")]
public string sDescricao { get; set; }
[JsonProperty(PropertyName = "Extras")]
public string sExtras { get; set; }
[JsonProperty(PropertyName = "Bundles")]
public List<Bundle> lstBundles { get; set; }
public void PreencherListaBundles(string jsonBundles)
{
List<Bundle> lstBundle = JsonConvert.DeserializeObject<List<Bundle>>(jsonBundles.ToString());
this.lstBundles = new List<Bundle>();
lstBundle.ForEach(x =>
{
if (!lstBundles.Select(y => y.sNome).Contains(x.sNome) && x.lstItens.Select(y => y.sNome).Contains(sNome))
{
lstBundles.Add(new Bundle() { sLocal = x.sLocal, sNome = x.sNome, sRecompensa = x.sRecompensa, lstItens = x.lstItens });
}
});
}
}
Bundle.cs
public class Bundle
{
[JsonProperty(PropertyName = "bundle")]
public string sNome { get; set; }
[JsonProperty(PropertyName = "location")]
public string sLocal { get; set; }
[JsonProperty(PropertyName = "reward")]
public string sRecompensa { get; set; }
[JsonProperty(PropertyName = "items")]
public List<BundleItem> lstItens { get; set; }
public class BundleItem
{
[JsonProperty(PropertyName = "name")]
public string sNome { get; set; }
[JsonProperty(PropertyName = "description")]
public string sDescricao { get; set; }
[JsonProperty(PropertyName = "quantity")]
public int nQuantidade { get; set; }
[JsonProperty(PropertyName = "quality")]
public string sQualidade { get; set; }
}
}
编辑:看起来某些机器上没有发生错误,就像你可以看到Gerard Sexton的回答一样,但是当我运行相同的代码时,我仍然得到48个结果。在此讨论中可以找到更多详细信息:http://chat.stackoverflow.com/rooms/106307/discussion-between-gerard-sexton-and-gabriel-duarte
答案 0 :(得分:0)
试试这个:
var result = JsonConvert.SerializeObject(lstItens, Formatting.Indented);
//Testing deserialization
var backResult = JsonConvert.DeserializeObject<List<Item>>(result);
结果变量是一个字符串,其中包含格式正确的字符串。
答案 1 :(得分:0)