我正在使用Newtonsoft.Json处理一些返回给我的JSON数据.根据我的要求,我可以得到一些看起来像:
{
"TotalRecords":2,
"Result":
[
{
"Id":24379,
"AccountName":"foo"
},
{
"Id":37209,
"AccountName":"bar"
}
],
"ResponseCode":0,
"Status":"OK",
"Error":"None"
}
Run Code Online (Sandbox Code Playgroud)
要么
{
"Result":
{
"Id":24379,
"AccountName":"foo"
},
"ResponseCode":0,
"Status":"OK",
"Error":"None"
}
Run Code Online (Sandbox Code Playgroud)
因此,有时"结果"是结果数组,或者"结果"可能是单个响应.
我尝试使用如何使用JSON.net处理同一属性的单个项目和数组的答案,但我仍然得到错误.
特别是我得到了一个
Newtonsoft.json.jsonSerializationException: 'Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List'...
Run Code Online (Sandbox Code Playgroud)
定制转换器看起来像:
public class SingleOrArrayConverter<T> : JsonConverter
{
public override bool CanConvert(Type objecType)
{
return (objecType == typeof(List<T>));
}
public override object ReadJson(JsonReader reader, Type objecType, object existingValue,
JsonSerializer serializer)
{
JToken token = JToken.Load(reader); …Run Code Online (Sandbox Code Playgroud) 我正在学习C#并遵循一些简单的指南.我遇到了问题而且不明白发生了什么.这是我的Visual Studio破解?
我有:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Learning_C_Sharp
{
class Program
{
static void Main(string[] args)
{
int[] myNumbers = { 10, 20, 30, 40, 50 };
for (int i = 0; i < myNumbers.Length; i++)
Console.WriteLine(myNumbers[i]);
//Console.WriteLine(myNumbers[0]);
Console.Read();
}
}
}
Run Code Online (Sandbox Code Playgroud)
我应该得到10,20,30,40,50的结果.但是我得到224作为我的第一个数字而不是10,无论我将第一个数字设置为.
我的Visual Studio刚刚坏了吗?
我已经卸载并重新安装,没有任何变化.创建一个新项目并手动键入代码而不进行任何更改.只要数组是int,第一个值就返回224.它也接受数学,如果我说要返回值并添加一个,那么我得到225.
屏幕截图也显示调试.

其他编辑:
我已经把它剥离到了骨头,不管我怎么做,我仍然得到相同的结果.
我已经卸载,重新安装,使用Revo Uninstaller来完全消除VS. 周二我要重新计算我的电脑,除非有人有想法.
当我打开实时测试时,我的测试显示“从实时单元测试中排除”。这仅在我使用 NUnit 时发生,使用 MSTest 工作正常。
我有:Visual Studio Enterprise 2017 (15.6.2) NUnit 3.10.1
一个简单的代码示例
using Microsoft.VisualStudio.TestTools.UnitTesting;
using NUnit.Framework;
namespace UnitTestProject1
{
[TestClass]
public class UnitTest1
{
[TestMethod]
public void TestMethod1()
{
}
}
[TestFixture]
public class NUnitTest1
{
[Test]
public void NUnitTestMethod1()
{
}
}
}
Run Code Online (Sandbox Code Playgroud)
TestMethod1()烧杯旁边有一个绿色复选标记,表示 Live Unit 测试已开启并被测试覆盖。
NUnitTestMethod1()有烧杯,但没有复选标记,将鼠标悬停在它上面显示“从实时单元测试中排除”。
我错过了什么?除了我通过 Nuget 添加 NUnit 并添加额外的测试类/方法之外,没有进行其他编辑或更改。我正在全新安装的 Visual Studio 上运行,其中唯一添加的其他内容是 ReSharper Ultimate (2017.3.2)。
在 ISerialized 中,Resharper 抱怨“仅使用了 'SerializeShape”的实现。有什么吗更多的我应该做的,或者是我的使用界面简单过杀在这种情况下?我的“要求”是任何对 Shape 类的使用都必须实现 SerializeShape。我试图以一种合理的、传统的方式使用 Interface,但也许我不是?
我有一个这样的界面:
namespace Shapes
{
internal interface ISerialized<in T>
{
string SerializeShape();
}
}
Run Code Online (Sandbox Code Playgroud)
我有一类这样的:
using System.Runtime.Serialization;
using Newtonsoft.Json;
namespace Shapes
{
[DataContract]
public class Shape : ISerialized<Shape>
{
[DataMember] public double Perimeter { get; set; }
[DataMember] public double Area { get; set; }
[DataMember] public string ShapeName { get; set; }
[DataMember] public string ShapeException { get; set; }
public string SerializeShape(Shape shape)
{
return JsonConvert.SerializeObject(shape, Formatting.Indented);
} …Run Code Online (Sandbox Code Playgroud)