识别属性的值是否为数组

时间:2015-01-20 00:37:16

标签: c# json json.net

我有一个JSON文件:

{  
   "abn":"63119059513",
   "acn":"119059513",
   "business_structure":"Private Company",
   "ngr_number":"1231231",
   "cbh_number":"1231231",
   "main_name":"Brickworks Building Products Pty Ltd",
   "trading_name":"Brickworks",
   "other_trading_names":"Austral Bricks",
   "directors":[  
      {  
         "ID":"12114",
         "ae_forms_filled_in_ID":"22739",
         "name":"John Smith",
         "dob":"1983-10-29",
         "address_line_1":"123 Fake Street",
         "address_line_2":"",
         "address_line_city":"Fakeland",
         "address_line_postcode":"2000",
         "address_line_state":"New South Wales",
         "address_line_country":"Australia",
         "order_extract_id":null,
         "director_found":null,
         "drivers_lic":"",
         "home_mortgage":"",
         "phone":"",
         "mobile":"",
         "director_email":"",
         "director_title":"Mr",
         "director_position":"Director",
         "dir_pdf_url":null
      }
   ],

}

我想确定任何属性的值是否具有数组结构。到目前为止我能想出的最好的是:

StreamReader streamrr = new StreamReader("C:\\temp\\agfarm_example_udate.json", Encoding.UTF8);

string JSON = streamrr.ReadToEnd();

JObject CWFile = JObject.Parse(JSON);
foreach (JProperty property in CWFile.Properties())
{
    // Do something

    if (property.Value.ToString().Contains("["))
    {
        // Do something with the array
        JArray items = (JArray)CWFile[property.Name];

        foreach (JObject o in items.Children<JObject>())
        {
            foreach (JProperty p in o.Properties())
            {
               // Do something
            }
        }
    }
}

为了确定属性值是否有数组,我使用了条件:

if (property.Value.ToString().Contains("["))

我只是想知道是否有更好的方法来进行此检查?

2 个答案:

答案 0 :(得分:10)

执行此操作的一种方法是检查JToken.Type属性。数组的类型为JTokenType.Array

if (property.Value.Type == JTokenType.Array)
{
    var items = (JArray)property.Value;

    // Proceed as before.
}

或者,您可以尝试转换为JArray

if (property.Value is JArray)
{
    var items = (JArray)property.Value;

    // Proceed as before.
}

两者都优于检查property.Value.ToString().Contains("["),因为嵌套属性可能具有数组值,从而导致括号出现在ToString()返回的某个位置。

如果要递归查找带有数组值的每个属性,可以引入扩展方法:

public static class JsonExtensions
{
    public static IEnumerable<JToken> WalkTokens(this JToken node)
    {
        if (node == null)
            yield break;
        yield return node;
        foreach (var child in node.Children())
            foreach (var childNode in child.WalkTokens())
                yield return childNode;
    }
}

然后做:

var CWFile = JToken.Parse(JSON)
var arrayProperties = CWFile.WalkTokens().OfType<JProperty>().Where(prop => prop.Value.Type == JTokenType.Array);

答案 1 :(得分:-1)

    public void Traverse(string name, JToken j)
    {
        foreach (JToken token in j.AsJEnumerable())
            if (token.Type == JTokenType.Object)
            {
                foreach (var pair in token as JObject)                        
                {
                    string name_ = pair.Key;
                    JToken child = pair.Value;
                    Traverse(name, child);
                }
            }
            else if (token.Type == JTokenType.Array) //an array property found 
            {
                foreach (var child in token.Children())
                    Traverse(((JProperty)j).Name, child);
            }
            else if (token.Type == JTokenType.Property)
            {
                var property = token as JProperty; //current level property
                Traverse(name, (JContainer)token);
            }
            else //current level property name & value
            {
                var nm = "";
                var t = "";
                if (j is JProperty)
                {
                    nm = ((JProperty)j).Name;
                    t = Convert.ToString(((JProperty)j).Value);
                }
                t = Convert.ToString(token);
            }
    }

呼叫:

JSON = JObject.Parse(...);
Traverse("", JSON);

解析和解析文本出于同样的原因并不是很明智:

if(property.Value.ToString()。Contains(“[”))

Json结构很简单:对象,数组,属性,值

http://www.json.org/

所以数组是json的一个众所周知的对象,我们正在寻找它:

if(token.Type == JTokenType.Array)//数组属性