如何从响应中获取键值,其中键值对的位置在每个请求中都在变化。

huangapple go评论66阅读模式
英文:

How to get the key value from response where the position of the key value pair are changing on each request

问题

有一个独特的问题,当我一遍又一遍地请求相同的API时,我得到的响应是相同的,但响应的键值对的值不断变化,值保持不变。

第一个请求的响应:

{
    "fields": {
        "customfield-4b7c5fb0-20de-4c4f-aecf-892b585855ab": "测试日程安排",
        "test_additional_history": "(0010,21B0)",
        "test_mother_birthname": "(0010,1060)",
        "customfield-73ee5b7f-5cfb-4cbb-ab09-69583de4b9cc": "您是否想要请求加急中央审查以确认进展?",
        "test_current_location": "(0038,0300)",
        "test_size": "(0010,1020)",
        "customfield-6844b23b-cff0-44b0-b2af-5eda668dc729": "这是基线测试吗?",
        "modality": "(0008,0060)",
        "date": "(0008,0020)",
        "customfield-d52ba358-07c1-428e-a345-b8bcb8d9b314": "受试者编号",
        "test_name": "(0010,0010)",
        "customfield-f049af7b-a550-45dc-8bae-abc27797518f": "备注",
        "user_occupation": "(0010,2180)",
        "user_birthname": "(0010,1005)",
        "user_size": "(0010,1020)",
        "customfield-5701c632-33a1-4e67-9c34-c0bbf3a82208": "试验测试名称",
        "user_occupation": "(0010,2180)",
        "user_birthname": "(0010,1005)",
        "user_size": "(0010,1020)",
        "customfield-26918767-1b73-413c-835d-ed204f1a3297": "测试手臂",
        "user_mother_birthname": "(0010,1060)",
        "customfield-a5363a01-b8b2-4da2-acfe-bbc2316dbbdd": "测试ID",
        "user_birth_time": "(0010,0032)",
        "customfield-8de944cd-ad93-490a-b34d-f8d248a70997": "测试开始日期",
        "user_address": "(0010,1040)",
        "customfield-5c0bb293-3f4b-42b5-aa49-b121d621a3dd": "随机化日期",
        "user_description": "(0008,1030)",
        "customfield-d378e755-6da9-4ba5-a8ef-41026272adda": "试验项目tims",
        "user_record_locator": "(0010,1090)"
    },
    "status": "OK",
    "defaults": {
        "customfield-d378e755-6da9-4ba5-a8ef-41026272adda": "PERF_TEST00004"
    }
}

第二个请求的响应:

{
    "status": "OK",
    "defaults": {
        "customfield-d378e755-6da9-4ba5-a8ef-41026272adda": "PERF_TEST00004"
    },
    "fields": {
        "customfield-d52ba358-07c1-428e-a345-b8bcb8d9b314": "受试者编号",
        "test_additional_history": "(0010,21B0)",
        "test_mother_birthname": "(0010,1060)",
        "customfield-d378e755-6da9-4ba5-a8ef-41026272adda": "试验项目tims",
        "test_current_location": "(0038,0300)",
        "test_size": "(0010,1020)",
        "customfield-26918767-1b73-413c-835d-ed204f1a3297": "测试手臂",
        "modality": "(0008,0060)",
        "date": "(0008,0020)",
        "customfield-8de944cd-ad93-490a-b34d-f8d248a70997": "测试开始日期",
        "test_name": "(0010,0010)",
        "customfield-5701c632-33a1-4e67-9c34-c0bbf3a82208": "试验测试名称",
        "user_occupation": "(0010,2180)",
        "user_birthname": "(0010,1005)",
        "user_size": "(0010,1020)",
        "customfield-5c0bb293-3f4b-42b5-aa49-b121d621a3dd": "随机化日期",
        "user_mother_birthname": "(0010,1060)",
        "customfield-6844b23b-cff0-44b0-b2af-5eda668dc729": "这是基线测试吗?",
        "user_birth_time": "(0010,0032)",
        "customfield-a5363a01-b8b2-4da2-acfe-bbc2316dbbdd": "测试ID",
        "user_address": "(0010,1040)",
        "customfield-f049af7b-a550-45dc-8bae-abc27797518f": "备注",
        "user_description": "(0008,1030)",
        "customfield-4b7c5fb0-20de-4c4f-aecf-892b585855ab": "测试日程安排",
        "user_record_locator": "(0010,1090)",
        "customfield-73ee5b7f-5cfb-4cbb-ab09-69583de4b9cc": "您是否想要请求加急中央审查以确认进展?"
    }
}

两个响应是相同的,只是值的位置改变了。我想获取特定值的键。

例如:

1. "customfield-4b7c5fb0-20de-4c4f-aecf-892b585855ab": "测试日程安排"
2. "customfield-6844b23b-cff0-44b0-b2af-5eda668dc729": "这是基线测试吗?"
3. "customfield-d52ba358-07c1-428e-a345-b8bcb8d9b314": "受试者编号"
4. "customfield-f049af7b

<details>
<summary>英文:</summary>

Have a unique problem, when I request the same api again and again I am getting the same response but the key-value pair of the response are keep on changing values are remain same. 

Response 1 on first request: 

    {
    	&quot;fields&quot;: {
    		&quot;customfield-4b7c5fb0-20de-4c4f-aecf-892b585855ab&quot;: &quot;Test Schedule&quot;,
    		&quot;test_additional_history&quot;: &quot;(0010,21B0)&quot;,
    		&quot;test_mother_birthname&quot;: &quot;(0010,1060)&quot;,
    		&quot;customfield-73ee5b7f-5cfb-4cbb-ab09-69583de4b9cc&quot;: &quot;Would you like to request an expedited central review to confirm progression?&quot;,
    		&quot;test_current_location&quot;: &quot;(0038,0300)&quot;,
    		&quot;test_size&quot;: &quot;(0010,1020)&quot;,
    		&quot;customfield-6844b23b-cff0-44b0-b2af-5eda668dc729&quot;: &quot;Is this a baseline test?&quot;,
    		&quot;modality&quot;: &quot;(0008,0060)&quot;,
    		&quot;date&quot;: &quot;(0008,0020)&quot;,
    		&quot;customfield-d52ba358-07c1-428e-a345-b8bcb8d9b314&quot;: &quot;Subject Number&quot;,
    		&quot;test_name&quot;: &quot;(0010,0010)&quot;,
    		&quot;customfield-f049af7b-a550-45dc-8bae-abc27797518f&quot;: &quot;Comments&quot;,
    		&quot;user_occupation&quot;: &quot;(0010,2180)&quot;,
    		&quot;user_birthname&quot;: &quot;(0010,1005)&quot;,
    		&quot;user_size&quot;: &quot;(0010,1020)&quot;,
    		&quot;customfield-5701c632-33a1-4e67-9c34-c0bbf3a82208&quot;: &quot;trial_test_name&quot;,
    		&quot;user_occupation&quot;: &quot;(0010,2180)&quot;,
    		&quot;user_birthname&quot;: &quot;(0010,1005)&quot;,
    		&quot;user_size&quot;: &quot;(0010,1020)&quot;,
    		&quot;customfield-26918767-1b73-413c-835d-ed204f1a3297&quot;: &quot;Test Arm&quot;,
    		&quot;user_mother_birthname&quot;: &quot;(0010,1060)&quot;,
    		&quot;customfield-a5363a01-b8b2-4da2-acfe-bbc2316dbbdd&quot;: &quot;Test ID&quot;,
    		&quot;user_birth_time&quot;: &quot;(0010,0032)&quot;,
    		&quot;customfield-8de944cd-ad93-490a-b34d-f8d248a70997&quot;: &quot;Test Start Date&quot;,
    		&quot;user_address&quot;: &quot;(0010,1040)&quot;,
    		&quot;customfield-5c0bb293-3f4b-42b5-aa49-b121d621a3dd&quot;: &quot;Randomization Date&quot;,
    		&quot;user_description&quot;: &quot;(0008,1030)&quot;,
    		&quot;customfield-d378e755-6da9-4ba5-a8ef-41026272adda&quot;: &quot;trial_project_tims&quot;,
    		&quot;user_record_locator&quot;: &quot;(0010,1090)&quot;
    	},
    	&quot;status&quot;: &quot;OK&quot;,
    	&quot;defaults&quot;: {
    		&quot;customfield-d378e755-6da9-4ba5-a8ef-41026272adda&quot;: &quot;PERF_TEST00004&quot;
    	}
    }

Response 2 on second request: 

    {
    	&quot;status&quot;: &quot;OK&quot;,
    	&quot;defaults&quot;: {
    		&quot;customfield-d378e755-6da9-4ba5-a8ef-41026272adda&quot;: &quot;PERF_TEST00004&quot;
    	},
    	&quot;fields&quot;: {
    		&quot;customfield-d52ba358-07c1-428e-a345-b8bcb8d9b314&quot;: &quot;Subject Number&quot;,
    		&quot;test_additional_history&quot;: &quot;(0010,21B0)&quot;,
    		&quot;test_mother_birthname&quot;: &quot;(0010,1060)&quot;,
    		&quot;customfield-d378e755-6da9-4ba5-a8ef-41026272adda&quot;: &quot;trial_project_tims&quot;,
    		&quot;test_current_location&quot;: &quot;(0038,0300)&quot;,
    		&quot;test_size&quot;: &quot;(0010,1020)&quot;,
    		&quot;customfield-26918767-1b73-413c-835d-ed204f1a3297&quot;: &quot;Test Arm&quot;,
    		&quot;modality&quot;: &quot;(0008,0060)&quot;,
    		&quot;date&quot;: &quot;(0008,0020)&quot;,
    		&quot;customfield-8de944cd-ad93-490a-b34d-f8d248a70997&quot;: &quot;Test Start Date&quot;,
    		&quot;test_name&quot;: &quot;(0010,0010)&quot;,
    		&quot;customfield-5701c632-33a1-4e67-9c34-c0bbf3a82208&quot;: &quot;trial_test_name&quot;,
    		&quot;user_occupation&quot;: &quot;(0010,2180)&quot;,
    		&quot;user_birthname&quot;: &quot;(0010,1005)&quot;,
    		&quot;user_size&quot;: &quot;(0010,1020)&quot;,
    		&quot;customfield-5c0bb293-3f4b-42b5-aa49-b121d621a3dd&quot;: &quot;Randomization Date&quot;,
    		&quot;user_mother_birthname&quot;: &quot;(0010,1060)&quot;,
    		&quot;customfield-6844b23b-cff0-44b0-b2af-5eda668dc729&quot;: &quot;Is this a baseline test?&quot;,
    		&quot;user_birth_time&quot;: &quot;(0010,0032)&quot;,
    		&quot;customfield-a5363a01-b8b2-4da2-acfe-bbc2316dbbdd&quot;: &quot;Test ID&quot;,
    		&quot;user_address&quot;: &quot;(0010,1040)&quot;,
    		&quot;customfield-f049af7b-a550-45dc-8bae-abc27797518f&quot;: &quot;Comments&quot;,
    		&quot;user_description&quot;: &quot;(0008,1030)&quot;,
    		&quot;customfield-4b7c5fb0-20de-4c4f-aecf-892b585855ab&quot;: &quot;Test Schedule&quot;,
    		&quot;user_record_locator&quot;: &quot;(0010,1090)&quot;,
    		&quot;customfield-73ee5b7f-5cfb-4cbb-ab09-69583de4b9cc&quot;: &quot;Would you like to request an expedited central review to confirm progression?&quot;
    	}
    }


Both the response are identical only the value&#39;s place are changed. I want to fetch the `key` of very specific value.

example: 

 1. &quot;customfield-4b7c5fb0-20de-4c4f-aecf-892b585855ab&quot;: &quot;Test Schedule&quot; 
 2. &quot;customfield-6844b23b-cff0-44b0-b2af-5eda668dc729&quot;: &quot;Is this a baseline test?&quot;
 3. &quot;customfield-d52ba358-07c1-428e-a345-b8bcb8d9b314&quot;: &quot;Subject Number&quot;
 4. &quot;customfield-f049af7b-a550-45dc-8bae-abc27797518f&quot;: &quot;Comments&quot;
 5. &quot;customfield-5701c632-33a1-4e67-9c34-c0bbf3a82208&quot;: &quot;trial_test_name&quot;
 6. &quot;customfield-26918767-1b73-413c-835d-ed204f1a3297&quot;: &quot;Test Arm&quot;
 7. &quot;customfield-a5363a01-b8b2-4da2-acfe-bbc2316dbbdd&quot;: &quot;Test ID&quot;
 8. &quot;customfield-d378e755-6da9-4ba5-a8ef-41026272adda&quot;: &quot;trial_project_tims&quot;

Want to fetch all the key value which is,

    customfield-4b7c5fb0-20de-4c4f-aecf-892b585855ab
    customfield-6844b23b-cff0-44b0-b2af-5eda668dc729
    customfield-d52ba358-07c1-428e-a345-b8bcb8d9b314
    customfield-f049af7b-a550-45dc-8bae-abc27797518f
    customfield-5701c632-33a1-4e67-9c34-c0bbf3a82208
    customfield-26918767-1b73-413c-835d-ed204f1a3297
    customfield-a5363a01-b8b2-4da2-acfe-bbc2316dbbdd
    customfield-d378e755-6da9-4ba5-a8ef-41026272adda

Used regular expression, 
for example for getting the subject number I used 

regex: `&quot;date&quot;:&quot;(.*?)&quot;,&quot;(.*?)&quot;:&quot;Subject Number&quot;`
       $1$
       2

Worked first time but failing second time as the position changed. Is there any way like using `JSON Extractor` or any other  with condition to find the keys based upon the value as value will be constant all the time ?





</details>


# 答案1
**得分**: 1

根据您的输出,以下是可以解决您的问题的正则表达式:

所以根据您的说法,这些值是常量,您需要相应的键,那么这些表达式将解决您的问题:

\s*&quot;([^&quot;]*)&quot;:?\s*&quot;测试计划&quot;
\s*&quot;([^&quot;]*)&quot;:?\s*&quot;这是一个
\s*&quot;([^&quot;]*)&quot;:?\s*&quot;受试者编号&quot;
\s*&quot;([^&quot;]*)&quot;:?\s*&quot;评论&quot;
\s*&quot;([^&quot;]*)&quot;:?\s*&quot;试验名称&quot;
\s*&quot;([^&quot;]*)&quot;:?\s*&quot;测试组&quot;
\s*&quot;([^&quot;]*)&quot;:?\s*&quot;测试ID&quot;
\s*&quot;([^&quot;]*)&quot;:?\s*&quot;试验项目Tims&quot;

<details>
<summary>英文:</summary>

According to your output these are the regular expression which should solve your problem,

So according to you the values are constant  and you need the corresponding keys then these expression  will solve your problem,

    \s*&quot;([^&quot;]*)&quot;:?\s*&quot;Test Schedule&quot;
    \s*&quot;([^&quot;]*)&quot;:?\s*&quot;Is this a
    \s*&quot;([^&quot;]*)&quot;:?\s*&quot;Subject Number&quot;
    \s*&quot;([^&quot;]*)&quot;:?\s*&quot;Comments&quot;
    \s*&quot;([^&quot;]*)&quot;:?\s*&quot;trial_test_name&quot;
    \s*&quot;([^&quot;]*)&quot;:?\s*&quot;Test Arm&quot;
    \s*&quot;([^&quot;]*)&quot;:?\s*&quot;Test ID&quot;
    \s*&quot;([^&quot;]*)&quot;:?\s*&quot;trial_project_tims&quot;

</details>



huangapple
  • 本文由 发表于 2023年7月14日 01:13:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/76681836.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定