JavaEar 专注于收集分享传播有价值的技术资料

无法访问JEnumerable 子项 (Can't access JEnumerable children)

问题描述


英文原文

if I try to call code below as it is, I receive

,"Name":"Newtonsoft.Json.Linq.JEnumerable`1[Newtonsoft.Json.Linq.JToken]"

as result. If I change the related line to

var property = myObject[propertyNames.Last()].FirstOrDefault();

then I receive

Cannot access child value on Newtonsoft.Json.Linq.JProperty

any idea about what the problem might be and/or workaround? Thanks...

Stack Trace

  • at Newtonsoft.Json.Linq.JToken.get_Item(Object key)
  • at Newtonsoft.Json.Linq.Extensions.d__4`2.MoveNext()
  • at System.Linq.Enumerable.d__14`2.MoveNext()
  • at Newtonsoft.Json.Linq.Extensions.d__f`2.MoveNext()
  • at Newtonsoft.Json.Linq.Extensions.d__4`2.MoveNext()
  • at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable`1 source)

Code

void Main()
{
    JObject objects = JObject.Parse("{\"Main\":[{\"Sub\":[{\"FieldName\":\"TEST\"}]}]}");
    Console.WriteLine (SetProperty(objects, "Name", "Main", "Sub", "FieldName"));
}

private static string SetProperty(JObject objects, string propertyKey, params string[] propertyNames)
{
    var myObject = objects.AsJEnumerable();

    for (int counter = 0; counter < propertyNames.Count()-1; counter++)
    {
        myObject = myObject[propertyNames[counter]].Children();
    }   
    var property = myObject[propertyNames.Last()];

    string propertyValue = property == null
                            ? string.Empty
                            : property.ToString();
    string output = string.Format(",\"{0}\":\"{1}\"", propertyKey, propertyValue);
    return output;
}

中文翻译

if I try to call code below as it is, I receive

,"Name":"Newtonsoft.Json.Linq.JEnumerable`1[Newtonsoft.Json.Linq.JToken]"

as result. If I change the related line to

var property = myObject[propertyNames.Last()].FirstOrDefault();

then I receive

Cannot access child value on Newtonsoft.Json.Linq.JProperty

any idea about what the problem might be and/or workaround? Thanks...

Stack Trace

  • at Newtonsoft.Json.Linq.JToken.get_Item(Object key)
  • at Newtonsoft.Json.Linq.Extensions.d__4`2.MoveNext()
  • at System.Linq.Enumerable.d__14`2.MoveNext()
  • at Newtonsoft.Json.Linq.Extensions.d__f`2.MoveNext()
  • at Newtonsoft.Json.Linq.Extensions.d__4`2.MoveNext()
  • at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable`1 source)

Code

void Main()
{
    JObject objects = JObject.Parse("{\"Main\":[{\"Sub\":[{\"FieldName\":\"TEST\"}]}]}");
    Console.WriteLine (SetProperty(objects, "Name", "Main", "Sub", "FieldName"));
}

private static string SetProperty(JObject objects, string propertyKey, params string[] propertyNames)
{
    var myObject = objects.AsJEnumerable();

    for (int counter = 0; counter < propertyNames.Count()-1; counter++)
    {
        myObject = myObject[propertyNames[counter]].Children();
    }   
    var property = myObject[propertyNames.Last()];

    string propertyValue = property == null
                            ? string.Empty
                            : property.ToString();
    string output = string.Format(",\"{0}\":\"{1}\"", propertyKey, propertyValue);
    return output;
}

if I try to call code below as it is, I receive

,"Name":"Newtonsoft.Json.Linq.JEnumerable`1[Newtonsoft.Json.Linq.JToken]"

as result. If I change the related line to

var property = myObject[propertyNames.Last()].FirstOrDefault();

then I receive

Cannot access child value on Newtonsoft.Json.Linq.JProperty

any idea about what the problem might be and/or workaround? Thanks...

Stack Trace

  • at Newtonsoft.Json.Linq.JToken.get_Item(Object key)
  • at Newtonsoft.Json.Linq.Extensions.d__4`2.MoveNext()
  • at System.Linq.Enumerable.d__14`2.MoveNext()
  • at Newtonsoft.Json.Linq.Extensions.d__f`2.MoveNext()
  • at Newtonsoft.Json.Linq.Extensions.d__4`2.MoveNext()
  • at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable`1 source)

Code

void Main()
{
    JObject objects = JObject.Parse("{\"Main\":[{\"Sub\":[{\"FieldName\":\"TEST\"}]}]}");
    Console.WriteLine (SetProperty(objects, "Name", "Main", "Sub", "FieldName"));
}

private static string SetProperty(JObject objects, string propertyKey, params string[] propertyNames)
{
    var myObject = objects.AsJEnumerable();

    for (int counter = 0; counter < propertyNames.Count()-1; counter++)
    {
        myObject = myObject[propertyNames[counter]].Children();
    }   
    var property = myObject[propertyNames.Last()];

    string propertyValue = property == null
                            ? string.Empty
                            : property.ToString();
    string output = string.Format(",\"{0}\":\"{1}\"", propertyKey, propertyValue);
    return output;
}

if I try to call code below as it is, I receive

,"Name":"Newtonsoft.Json.Linq.JEnumerable`1[Newtonsoft.Json.Linq.JToken]"

as result. If I change the related line to

var property = myObject[propertyNames.Last()].FirstOrDefault();

then I receive

Cannot access child value on Newtonsoft.Json.Linq.JProperty

any idea about what the problem might be and/or workaround? Thanks...

Stack Trace

  • at Newtonsoft.Json.Linq.JToken.get_Item(Object key)
  • at Newtonsoft.Json.Linq.Extensions.d__4`2.MoveNext()
  • at System.Linq.Enumerable.d__14`2.MoveNext()
  • at Newtonsoft.Json.Linq.Extensions.d__f`2.MoveNext()
  • at Newtonsoft.Json.Linq.Extensions.d__4`2.MoveNext()
  • at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable`1 source)

Code

void Main()
{
    JObject objects = JObject.Parse("{\"Main\":[{\"Sub\":[{\"FieldName\":\"TEST\"}]}]}");
    Console.WriteLine (SetProperty(objects, "Name", "Main", "Sub", "FieldName"));
}

private static string SetProperty(JObject objects, string propertyKey, params string[] propertyNames)
{
    var myObject = objects.AsJEnumerable();

    for (int counter = 0; counter < propertyNames.Count()-1; counter++)
    {
        myObject = myObject[propertyNames[counter]].Children();
    }   
    var property = myObject[propertyNames.Last()];

    string propertyValue = property == null
                            ? string.Empty
                            : property.ToString();
    string output = string.Format(",\"{0}\":\"{1}\"", propertyKey, propertyValue);
    return output;
}

1个回答

    最佳答案

  1. 英文原文

    I'm making some assumptions about how your data is structured. Also, you really shouldn't build JSON manually, which you appear to be doing at the end there. Instead, I'm returning a KeyValuePair that can be added to a dictionary for serialization.

    private static KeyValuePair<string, string> GetProperty(JObject objects,
                        string propertyKey, params string[] propertyNames)
    {
        JToken token = objects[propertyNames.First()];
        foreach (var name in propertyNames.Skip(1))
            token = token[0][name];
        return new KeyValuePair<string, string>(propertyKey, (string)token);
    }
    // returns [Name, TEST]
    

    中文翻译

    我对数据的结构做了一些假设。此外,你真的不应该手动构建JSON,你似乎在那里做了。相反,我正在返回一个 KeyValuePair ,可以添加到字典中进行序列化。

      private static KeyValuePair&lt; string,string&gt; GetProperty(JObject对象,
                        string propertyKey,params string [] propertyNames)
    {
        JToken token = objects [propertyNames.First()];
        foreach(propertyNames.Skip(1)中的var名称)
            token = token [0] [name];
        返回new KeyValuePair&lt; string,string&gt;(propertyKey,(string)token);
    }
    //返回[Name,TEST]
     

    I'm making some assumptions about how your data is structured. Also, you really shouldn't build JSON manually, which you appear to be doing at the end there. Instead, I'm returning a KeyValuePair that can be added to a dictionary for serialization.

    private static KeyValuePair<string, string> GetProperty(JObject objects,
                        string propertyKey, params string[] propertyNames)
    {
        JToken token = objects[propertyNames.First()];
        foreach (var name in propertyNames.Skip(1))
            token = token[0][name];
        return new KeyValuePair<string, string>(propertyKey, (string)token);
    }
    // returns [Name, TEST]
    

    我对数据的结构做了一些假设。此外,你真的不应该手动构建JSON,你似乎在那里做了。相反,我正在返回一个 KeyValuePair ,可以添加到字典中进行序列化。

      private static KeyValuePair&lt; string,string&gt; GetProperty(JObject对象,
                        string propertyKey,params string [] propertyNames)
    {
        JToken token = objects [propertyNames.First()];
        foreach(propertyNames.Skip(1)中的var名称)
            token = token [0] [name];
        返回new KeyValuePair&lt; string,string&gt;(propertyKey,(string)token);
    }
    //返回[Name,TEST]