gpt4 book ai didi

C#:无法检查类型是否为数组

转载 作者:太空狗 更新时间:2023-10-30 01:16:39 25 4
gpt4 key购买 nike

我一直在制作一个自定义库,用于将对象数组转换为 json 字符串并发布它。我最近遇到了无法检测到内部阵列的问题。

public static string ConvertArray( params object[] jsonData ) {
if( jsonData.Length % 2 != 0 ) throw new ArgumentException( string.Format( "Key \'{0}\' missing value", jsonData[jsonData.Length - 1] ) );
string json = "{";
for( int i = 0; i < jsonData.Length; ) {
json += string.Format( "\"{0}\":", jsonData[i++] );
if( jsonData[i] is string ) {
Console.WriteLine( "Found a string" );
json += string.Format( "\"{0}\",", jsonData[i++] );
} else if( jsonData[i].GetType( ).IsArray ) {
Console.WriteLine( "Found an internal array" );
json += string.Format( "{0},", ConvertArray( jsonData[i++] ) );
} else {
if( jsonData[i] is byte || jsonData[i] is sbyte || jsonData[i] is int || jsonData[i] is uint || jsonData[i] is short || jsonData[i] is ushort || jsonData[i] is long || jsonData[i] is ulong || jsonData[i] is float || jsonData[i] is double || jsonData[i] is bool || jsonData[i] is decimal ) {
Console.WriteLine( "Found a generic" );
json += string.Format( "{0},", jsonData[i++] );
} else if( jsonData[i] is char ) {
Console.WriteLine( "Found a char" );
json += string.Format( "\'{0}\',", jsonData[i++] );
} else {
Console.WriteLine( "Found an object" );
object work = jsonData[i++];
try {
MethodInfo workMethod = work.GetType().GetMethod("ToJsonString");
json += string.Format( "{0},", workMethod.Invoke( work, new object[] { } ) );
} catch {
try {
using( MemoryStream memStr = new MemoryStream( ) ) {
new BinaryFormatter( ).Serialize( memStr, work );
memStr.Seek( 0, SeekOrigin.Begin );
using( StreamReader strReader = new StreamReader( memStr ) ) {
json += string.Format( "\"{0}\",", strReader.ReadToEnd( ) );
}
}
} catch {
throw new ArgumentException( "The value for key \'{0}\' does not contain a public method ToJsonString and cannot be serialized" );
}
}
}
}
}
return json + "}";
}

我一直在运行它:

string jsonString = JsonWebUtil.ConvertArray( new object[] {
"jsonrpc","2.0",
"method","generateIntegers",
"params",JsonWebUtil.ConvertArray( new object[] {
"apiKey", "<REDACTED>",
"n", 14,
"min", 0,
"max", 10,
} ),
"id", 10461
} );

我得到以下输出:

{"jsonrpc":"2.0","method":"generateIntegers","params":"{"apiKey":"<REDACTED>","n":14,"min":0,"max":10,}","id":10461,}

内部数组不应该有引号,但有。当它运行我的代码时,控制台显示它被检测为我声明为泛型的内容。我不知道为什么 jsonData[i].getType().IsArray 没有返回正确的值。

最佳答案

您的参数都不是数组 - 它们都是字符串或数字。您可能认为 是数组的参数不是。由于您将其包装在对 JsonWebUtil.ConvertArray 的调用中,它是否已转换为字符串。

我想你想要这样的东西:

string jsonString = JsonWebUtil.ConvertArray( new object[] {
"jsonrpc","2.0",
"method","generateIntegers",
"params", new object[] {
"apiKey", "<REDACTED>",
"n", 14,
"min", 0,
"max", 10,
} ,
"id", 10461
} );

其他可能会误导你的事情:

  • 包含引号的字符串
  • 捕获异常以确定不存在 ToJsonString 方法 - 为什么不检查 workMethod 是否为 null?

关于C#:无法检查类型是否为数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34553296/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com