簡體   English   中英

如何檢查Jarray的數據類型? (C#)

[英]How do i check the datatype of my Jarray? (C#)

我有一個接收JProperty數組的方法。 這可以是一個簡單的字符串數組:(“ Img1.png”,“ Img2.png”等)。 或包含對象的數組:

{[{
"id": "1",
"name": "name",
"image": "img1.png"},{
"id": "2",
"name": "name",
"image": "img2.png"},
{
"id": "3",
"name": "name",
"image": "img3.png"
}]}"

在方法中,需要執行JProperty的不同操作,但是我無法獲得if語句來過濾對象,也無法獲得對象事件。

目前這是我的代碼:

private static void handleArray(JProperty array)
{

    foreach (JArray x in array)
    {
        JTokenType type = x.Type;
        if (type == JTokenType.Object)
        {
            Console.WriteLine("Array with objects!");
        }
        else { 
            foreach (string childrensTokens in x)
              //Array with normal strings
                Console.WriteLine(childrensTokens);
        }
    }
}

(else語句因為也接收對象而使atm崩潰。)有人知道如何幫助我嗎? 我試圖到達childrensTokens,但失敗了。

使用以下方式修復了該問題:

 private static void handleArray(JProperty array)
    {
        //voor de gewone array:

        foreach (JArray x in array)
        {
            foreach (var a in x)
                if(a.Type == JTokenType.Object)
                {
                    Console.WriteLine("Array with objects!");
                }
                else
                {
                    Console.WriteLine((string) a);
                }


        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM