代码无法从JSON中找到特定值

金翼

我使用C#在axosoft的软件调用OnTime上进行api调用。

我在客户端中创建了三个自定义字段:

custom_170

custom_171

custom_172

每个字段都会添加到JSON调用“ custom_fields”的一个部分中。OnTime提供了自己的API包装器,可轻松使用其代码。

在下面的C#代码中,我使用get从“ Defects”中提取JSON,然后循环遍历查找缺陷编号7。

如果找到数字7,它将从JSON ID,名称,custom_170,custom_171,custom_172中提取5个值。

我遇到的问题是我的程序找到了Id,Name,custom_170,但是查找custom_171的if语句从custom_172获取值,而最后一个if似乎从未被触摸过(请参见下面的结果)。

我怎样才能从custom_171和custom_172中获取值,并将它们放在正确的位置?

JSON(摘要)

{
    "data": {
        "reported_date": "2014-09-25T04:00:00Z",
        "percent_complete": 100,
        "archived": false,
        "publicly_viewable": false,
        "completion_date": null,
        "due_date": null,
        "description": "",
        "name": "Defect Created from API Explorer 3",
        "notes": "",
        "number": "7",
        "custom_fields": {
            "custom_171": "Work Around Steps",
            "custom_172": "Work Journal",
            "custom_170": "Analysis"
        }
    }
}

C#代码

        var DefectInfo = axosoftClient.Defects.Get();

        int? defectID = 0;
        string defectName = "";
        string defectAnalysis = "";
        string defectWAS = "";
        string defectWJ = "";

        foreach (var defect in DefectInfo.Data)
        {
            if(defect.Id == 7)
            {
                defectID = defect.Id;
                defectName = defect.Name;
                if(defect.CustomFields.ContainsKey("custom_170"))
                {
                    defectAnalysis = (string)defect.CustomFields["custom_170"];
                }
                if(defect.CustomFields.ContainsKey("custom_171"))
                {
                    defectWAS = (string)defect.CustomFields["custom_171"];
                }
                if (defect.CustomFields.ContainsKey("custom_172"))
                {
                    defectWAS = (string)defect.CustomFields["custom_172"];
                }

            } 
        }
        Console.WriteLine("Defect ID: {0} Defect Name: {1}\nAnalysis: {2} \nWork Around: {3}\nWork Journal: {4}\n\n", defectID, defectName, defectAnalysis, defectWAS, defectWJ);

结果

Defect ID: 7 Defect Name: Defect Created from API Explorer 3
Analysis: Analysis
Work Around: Work Journal
Work Journal:
品尝

defectWAS分配了两次,而defectWJ没有分配新值

您可能的意思是:

if (defect.CustomFields.ContainsKey("custom_172"))
{
    defectWJ = (string)defect.CustomFields["custom_172"];
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

无法使用indexOf方法从数组中找到特定值

来自分类Dev

无法在项目列表中找到特定的单词

来自分类Dev

无法从mysql的数组中找到特定的元素

来自分类Dev

从var值中找到特定的模式

来自分类Dev

无法使用在 stackoverflow 中找到的 VBA 代码解析 json 字符串

来自分类Dev

在json对象的键中找到值

来自分类Dev

如何在javascript中的特定行号中找到源代码?

来自分类Dev

无法在代码块中的C ++中找到浮动的机器epsilon

来自分类Dev

无法在python中的素数代码中找到错误

来自分类Dev

无法在python中的素数代码中找到错误

来自分类Dev

Wordpress - 无法在页面中找到 JS 代码片段

来自分类Dev

Visual Studio 2017 Professional - 无法在源代码中找到包

来自分类Dev

无法在我的代码中找到错误,使用 GCC 出现错误

来自分类Dev

BinarySearch无法在字符串[]中找到特定元素

来自分类Dev

循环直到在表中找到2个特定值?

来自分类Dev

循环直到在表中找到2个特定值?

来自分类Dev

在R中找到特定年份的最接近值

来自分类Dev

如何在映射中找到包含特定值的键?

来自分类Dev

python在数据框中找到特定的值

来自分类Dev

在列表中找到特定属性值的频率<对象>

来自分类Dev

在R中找到达到特定值的天数

来自分类Dev

在向量的排序向量中找到特定值的索引范围

来自分类Dev

在距离数据框中找到特定值的位置

来自分类Dev

SQLite:SQL 不会在字段中找到特定值

来自分类Dev

Angular4 如何从数组中找到特定值

来自分类Dev

可变函数jQuery无法从其他函数中找到值

来自分类Dev

sbt测试文件无法从src文件中找到值

来自分类Dev

可变函数jQuery无法从其他函数中找到值

来自分类Dev

如何防止在json数据中找到的特定对象上循环

Related 相关文章

  1. 1

    无法使用indexOf方法从数组中找到特定值

  2. 2

    无法在项目列表中找到特定的单词

  3. 3

    无法从mysql的数组中找到特定的元素

  4. 4

    从var值中找到特定的模式

  5. 5

    无法使用在 stackoverflow 中找到的 VBA 代码解析 json 字符串

  6. 6

    在json对象的键中找到值

  7. 7

    如何在javascript中的特定行号中找到源代码?

  8. 8

    无法在代码块中的C ++中找到浮动的机器epsilon

  9. 9

    无法在python中的素数代码中找到错误

  10. 10

    无法在python中的素数代码中找到错误

  11. 11

    Wordpress - 无法在页面中找到 JS 代码片段

  12. 12

    Visual Studio 2017 Professional - 无法在源代码中找到包

  13. 13

    无法在我的代码中找到错误,使用 GCC 出现错误

  14. 14

    BinarySearch无法在字符串[]中找到特定元素

  15. 15

    循环直到在表中找到2个特定值?

  16. 16

    循环直到在表中找到2个特定值?

  17. 17

    在R中找到特定年份的最接近值

  18. 18

    如何在映射中找到包含特定值的键?

  19. 19

    python在数据框中找到特定的值

  20. 20

    在列表中找到特定属性值的频率<对象>

  21. 21

    在R中找到达到特定值的天数

  22. 22

    在向量的排序向量中找到特定值的索引范围

  23. 23

    在距离数据框中找到特定值的位置

  24. 24

    SQLite:SQL 不会在字段中找到特定值

  25. 25

    Angular4 如何从数组中找到特定值

  26. 26

    可变函数jQuery无法从其他函数中找到值

  27. 27

    sbt测试文件无法从src文件中找到值

  28. 28

    可变函数jQuery无法从其他函数中找到值

  29. 29

    如何防止在json数据中找到的特定对象上循环

热门标签

归档