Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将LINQ转换为JSON查询从c转换为VB.NET后,如何解决InvalidCastException?_C#_Json_Vb.net_Linq - Fatal编程技术网

C# 将LINQ转换为JSON查询从c转换为VB.NET后,如何解决InvalidCastException?

C# 将LINQ转换为JSON查询从c转换为VB.NET后,如何解决InvalidCastException?,c#,json,vb.net,linq,C#,Json,Vb.net,Linq,我试图使用从到的代码,但我的项目是VB.NET。 我尝试了一些在线转换器,但没有多大成功。C版本工作得很好,但是VB.NET版本抛出了一个InvalidCastException。有什么问题吗 我的JSON: { "Response": [ [ { "id": 136662306, "symbol": "aaa", "status": "ACTIVE", "base": "731.07686321",

我试图使用从到的代码,但我的项目是VB.NET。 我尝试了一些在线转换器,但没有多大成功。C版本工作得很好,但是VB.NET版本抛出了一个InvalidCastException。有什么问题吗

我的JSON:

  {
  "Response": [
    [
      {
        "id": 136662306,
        "symbol": "aaa",
        "status": "ACTIVE",
        "base": "731.07686321",
        "amount": "6.95345994",
        "timestamp": "1524781083.0",
        "swap": "0.0",
        "pl": "5127.4352653395923394"
      },
      {
        "id": 137733525,
        "symbol": "bbb",
        "status": "ACTIVE",
        "base": "636.75093128",
        "amount": "1.1",
        "timestamp": "1531902193.0",
        "swap": "0.0",
        "pl": "687.800226608"
      }
    ]
  ]
}
我的C版本可以正常工作

JObject obj = JObject.Parse(json);

// Collect column titles: all property names whose values are of type JValue, distinct, in order of encountering them.
var values = obj.DescendantsAndSelf()
    .OfType<JProperty>()
    .Where(p => p.Value is JValue)
    .GroupBy(p => p.Name)
    .ToList();

var columns = values.Select(g => g.Key).ToArray();

// Filter JObjects that have child objects that have values.
var parentsWithChildren = values.SelectMany(g => g).SelectMany(v => v.AncestorsAndSelf().OfType<JObject>().Skip(1)).ToHashSet();

// Collect all data rows: for every object, go through the column titles and get the value of that property in the closest ancestor or self that has a value of that name.
var rows = obj
    .DescendantsAndSelf()
    .OfType<JObject>()
    .Where(o => o.PropertyValues().OfType<JValue>().Any())
    .Where(o => o == obj || !parentsWithChildren.Contains(o)) // Show a row for the root object + objects that have no children.
    .Select(o => columns.Select(c => o.AncestorsAndSelf()
        .OfType<JObject>()
        .Select(parent => parent[c])
        .Where(v => v is JValue)
        .Select(v => (string)v)
        .FirstOrDefault())
        .Reverse() // Trim trailing nulls
        .SkipWhile(s => s == null)
        .Reverse());

// Convert to CSV
var csvRows = new[] { columns }.Concat(rows).Select(r => string.Join(",", r));
var csv = string.Join("\n", csvRows);
例外情况

{"Unable to cast object of type 'WhereSelectEnumerableIterator`2[Newtonsoft.Json.Linq.JObject,System.Collections.Generic.IEnumerable`1[System.String]]' to type 'System.Collections.Generic.IEnumerable`1[System.String[]]'."}
为了使{columns}.Concatrows正常工作,似乎需要添加对的显式调用,以确保正确推断的TSource类型:

Dim csvRows={columns.AsEnumerable}.Concatrows_ .SelectFunctionr String.Join,,r 固定小提琴1

DirectCast{columns},IEnumerableOf IEnumerableOf String似乎也能工作,如中所述:

Dim csvRows=DirectCast{columns},IEnumerableOf IEnumerableOf String.Concatrows_ .SelectFunctionr String.Join,,r 固定小提琴2

显式调用Enumerable.ConcatOf IEnumerableOf String,而不使用推断,也可以:

Dim csvRows=Enumerable.ConcatOf IEnumerableOf字符串{columns},行_ .SelectFunctionr String.Join,,r 固定小提琴3

因此,您的整个代码应该如下所示:

作为JObject=JObject.Parsejson的Dim obj Dim values=obj.Degenants和Self.OfTypeOf JProperty.WhereFunctionp TypeOf p.Value为JValue.GroupByFunctionp p.Name.ToList 尺寸列=值。[选择]函数g.Key.ToArray Dim parentsWithChildren=values.SelectManyFunction g.SelectManyFunction v.Ancestors和Self.Of类型的JObject.Skip1.ToHashSet 尺寸行=对象后代和自身_ .工作对象的类型_ .wherefunction o.propertyvalue.OfTypeOf JValue.Any_ .o.Equalsobj或LSE的功能不包括子女的父母。也包括_ .selectfunction o columns.selectfunction c_ o、 安塞斯托尔和赛尔夫_ .工作对象的类型_ .selectFunctionParentC_ .JValue的类型_ .选择Functionv CStrv_ .第一道防线_ .反向_ .SkipWhileFunctions什么都不是_ 颠倒 Dim csvRows={columns.AsEnumerable}.Concatrows_ .SelectFunctionr String.Join,,r Dim csv=String.JoinvbLf,csvRows
我不知道VB是从我的屁股,但这行没有错,如果arr是什么,那么obj=JObject.Parsearr.ToString。如果array为null,那么array.tostring???也许您可以在C中添加一个新库,解决方案可以同时使用这两个lang。顺便说一句,如果您不知道JSON根容器是数组还是对象,请使用JToken.Parse,然后强制转换到JCContainer来执行Degenants和Self。有关详细信息,请参阅。我遵循了您为C代码提供的链接,但我没有看到任何与您正在显示的VB.NET代码相匹配的内容。您能提供C代码吗?您需要执行{columns.AsEnumerable}.Concatrows,而不仅仅是{columns}.Concatrows,请参阅。但是,我无法真正解释为什么VB.NET的类型推断在使用Linq的Concat扩展方法将字符串数组与延迟可枚举字符串组合时会出现一些奇怪的情况。添加ToEnumerable修复了类型推断,尽管它实际上没有做任何事情,但它只是一个向上投射。看见