JSON解析使用javascript提取特定子节点[已关闭]

mbzjlibv  于 2023-03-06  发布在  Java
关注(0)|答案(3)|浏览(122)
    • 已关闭**。此问题需要超过focused。当前不接受答案。
    • 想要改进此问题吗?**更新此问题,使其仅关注editing this post的一个问题。

6小时前关门了。
Improve this question
我有以下JSON对象

{
  "JOBS": [{
    "JOBNAME": "Online VAT Reporting for Spain",
    "JOBPATH": "/oracle/apps/ess/financials/europeanLocalizations/financialReporting",
    "REQUESTID": "1234",
    "STATUS": "SUCCEEDED",
    "CHILD": [{
      "JOBNAME": "Collections for the Issued Invoices Register",
      "JOBPATH": "/oracle/apps/ess/financials/europeanLocalizations/financialReporting",
      "REQUESTID": "5678",
      "STATUS": "SUCCEEDED"
    }, {
      "JOBNAME": "Invoices for the Issued Invoices Register",
      "JOBPATH": "/oracle/apps/ess/financials/europeanLocalizations/financialReporting",
      "REQUESTID": "0923444",
      "STATUS": "SUCCEEDED"
    }]
  }, {
    "JOBNAME": "Upload Interface Error and Job Output File to Universal Content Management",
    "JOBPATH": "/oracle/apps/ess/financials/commonModules/shared/common/interfaceLoader",
    "REQUESTID": "0939393",
    "STATUS": "SUCCEEDED"
  }],
  "SUMMARYSTATUS": "SUCCEEDED",
  "DOCUMENTID": "4848488",
  "DOCUMENTNAME": "XXXX_OnlineVatReporting_1897731"
}

使用javascript,我想提取具有"JOBNAME":"Invoices for the Issued Invoices Register"的子节点的REQUESTID在本例中是0923444
谁能告诉我如何获得REQUESTID

ctrmrzij

ctrmrzij1#

我建议阅读更多关于内置数组方法的内容,比如find:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find
但这应该可以

const jobsJSON = $theJSON
const requestId = jobs["JOBS"].flatMap(_jobs => _jobs.CHILD && _jobs.CHILD.find(job => job.JOBNAME === "Invoices for the Issued Invoices Register"))[0]['REQUESTID']
jhdbpxl9

jhdbpxl92#

检查以下内容:

const data = {
  "JOBS": [
    {
      "JOBNAME": "Online VAT Reporting for Spain",
      "JOBPATH": "/oracle/apps/ess/financials/europeanLocalizations/financialReporting",
      "REQUESTID": "1234",
      "STATUS": "SUCCEEDED",
      "CHILD": [
        {
          "JOBNAME": "Collections for the Issued Invoices Register",
          "JOBPATH": "/oracle/apps/ess/financials/europeanLocalizations/financialReporting",
          "REQUESTID": "5678",
          "STATUS": "SUCCEEDED"
        },
        {
          "JOBNAME": "Invoices for the Issued Invoices Register",
          "JOBPATH": "/oracle/apps/ess/financials/europeanLocalizations/financialReporting",
          "REQUESTID": "0923444",
          "STATUS": "SUCCEEDED"
        }
      ]
    },
    {
      "JOBNAME": "Upload Interface Error and Job Output File to Universal Content Management",
      "JOBPATH": "/oracle/apps/ess/financials/commonModules/shared/common/interfaceLoader",
      "REQUESTID": "0939393",
      "STATUS": "SUCCEEDED"
    }
  ],
  "SUMMARYSTATUS": "SUCCEEDED",
  "DOCUMENTID": "4848488",
  "DOCUMENTNAME": "XXXX_OnlineVatReporting_1897731"
};

let parent = "Online VAT Reporting for Spain";
let child = "Collections for the Issued Invoices Register";

const result = data.JOBS.find(job => job.JOBNAME === parent)
if(result) {
  if(child != "") {
    const childResult = result.CHILD.find(job => job.JOBNAME === child);
    if(childResult) {
      console.log(childResult.REQUESTID); // Output: 5678
    } else {
      console.log("Job not found");
    }
  } else {
    console.log(result.REQUESTID); // Output: 1234
  }
} else {
  console.log("Job not found");
}
ehxuflar

ehxuflar3#

// Parse the JSON string into a JavaScript object
const data = JSON.parse('{"JOBS":[{"JOBNAME":"Online VAT Reporting for Spain","JOBPATH":"/oracle/apps/ess/financials/europeanLocalizations/financialReporting","REQUESTID":"1234","STATUS":"SUCCEEDED","CHILD":[{"JOBNAME":"Collections for the Issued Invoices Register","JOBPATH":"/oracle/apps/ess/financials/europeanLocalizations/financialReporting","REQUESTID":"5678","STATUS":"SUCCEEDED"},{"JOBNAME":"Invoices for the Issued Invoices Register","JOBPATH":"/oracle/apps/ess/financials/europeanLocalizations/financialReporting","REQUESTID":"0923444","STATUS":"SUCCEEDED"}]},{"JOBNAME":"Upload Interface Error and Job Output File to Universal Content Management","JOBPATH":"/oracle/apps/ess/financials/commonModules/shared/common/interfaceLoader","REQUESTID":"0939393","STATUS":"SUCCEEDED"}],"SUMMARYSTATUS":"SUCCEEDED","DOCUMENTID":"4848488","DOCUMENTNAME":"XXXX_OnlineVatReporting_1897731"}');

// Loop through the JOBS array to find the job with "JOBNAME":"Invoices for the Issued Invoices Register"
let requestId;
data.JOBS.forEach(job => {
  job.CHILD.forEach(childJob => {
    if (childJob.JOBNAME === "Invoices for the Issued Invoices Register") {
      requestId = childJob.REQUESTID;
    }
  });
});

console.log(requestId); // Output: "0923444"

一种方法是:
1.首先使用JSON.parse()将JSON字符串解析为JavaScript对象。
1.然后,循环JOBS数组以查找具有**“JOBNAME”的作业:“Invoices for the Issued Invoices Register”
1.找到后,将其
REQUESTID值存储在requestId变量中。
1.最后,当输出
requestId**变量到控制台时,您可以得到您想要的。

相关问题