将数组中的Json对象解析为关系对象和字段

时间:2017-03-16 13:43:41

标签: arrays json postgresql pgadmin-4

您好我需要将operationsList数组下的json对象解析为关系字段和对象,但这里的技巧是我粘贴的示例在operationsList数组下有七个对象,我的代码能够单独解析每个json对象但是U需要使用能够自动选择每个json对象的函数。

我需要在pgadmin $中为postgres 9.6执行答案

注意:我使用所有功能

尝试了数小时

下面的代码

{
  "header" : {
    "tasklistid" : "RC016753",
    "planningPlant" : "ECL",
    "status" : "Z03",
    "title" : "725307-STG 1 LPT NZZ",
    "parentRouterId" : "",
    "enginecode" : "5B",
    "structureNode" : "5B_72530015200",
    "routerType" : "INR",
    "disassemblyLocation" : "TL725300",
    "latestReleaseDate" : "31.12.8888",
    "planningReference" : "INR-725300"
  },
  "operationsList" : [ {
    "order" : "0010",
    "text" : "232-003-IN PROCESS - FPI",
    "workCenter" : "248",
    "controlKey" : "ZQP3",
    "details" : {
      "longText" : "232-003-IN PROCESS - FPI FPI : Floroscent penetration Inspection This is Long text to test the API",
      "MICS" : [ {
        "order" : "0010",
       "MICNumber" : "C0000100",
        "Plant" : "ECL",
        "version" : "000003"
      }, {
        "order" : "0020",
        "MICNumber" : "C0175399",
        "Plant" : "ECL",
        "version" : "000001"
      }, {
        "order" : "0030",
        "MICNumber" : "C0175400",
        "Plant" : "ECL",
        "version" : "000001"
      } ],
      "dependencies" : [ ],
      "documents" : [ {
        "Number" : "5B/ZSC-ECL-0041176",
        "Type" : "ZSC",
        "version" : "00",
        "order" : "0500"
      }, {
        "Number" : "5B/ZSC-ECL-0041175",
        "Type" : "ZSC",
        "version" : "00",
        "order" : "0510"
      }, {
        "Number" : "5B/ZSC-ECL-0041177",
        "Type" : "ZSC",
        "version" : "00",
        "order" : "0520"
      }, {
        "Number" : "5B/ZSC-ECL-0041178",
        "Type" : "ZSC",
        "version" : "00",
        "order" : "0530"
      } ]
    }
  }, {
    "order" : "0020",
    "text" : "307-004-BENCHING",
    "workCenter" : "358",
    "controlKey" : "SM01",
    "details" : {
      "longText" : "",
      "MICS" : [ ],
      "dependencies" : [ ],
      "documents" : [ ]
    }
  }, {
    "order" : "0030",
    "text" : "110-005-CHEMICAL CLEANING",
    "workCenter" : "048",
    "controlKey" : "ZQP1",
    "details" : {
      "longText" : "",
      "MICS" : [ {
        "order" : "0010",
        "MICNumber" : "C0006683",
        "Plant" : "ECL",
        "version" : "000001"
      } ],
      "dependencies" : [ ],
      "documents" : [ ]
    }
  }, {
    "order" : "0040",
    "text" : "310-001-TIG WELDING",
    "workCenter" : "295",
    "controlKey" : "ZQP1",
    "details" : {
      "longText" : "",
      "MICS" : [ {
        "order" : "0010",
        "MICNumber" : "C0020962",
        "Plant" : "ECL",
        "version" : "000001"
      }, {
        "order" : "0020",
        "MICNumber" : "C0006552",
        "Plant" : "ECL",
        "version" : "000001"
      }, {
        "order" : "0030",
        "MICNumber" : "C0006511",
        "Plant" : "ECL",
        "version" : "000001"
      } ],
      "dependencies" : [ ],
      "documents" : [ {
        "Number" : "5B/ZSC-ECL-0041175",
        "Type" : "ZSC",
        "version" : "00",
        "order" : "0500"
      }, {
        "Number" : "5B/ZSC-ECL-0041176",
        "Type" : "ZSC",
        "version" : "00",
        "order" : "0510"
      }, {
        "Number" : "5B/ZSC-ECL-0041177",
        "Type" : "ZSC",
        "version" : "00",
        "order" : "0520"
      }, {
        "Number" : "5B/ZSC-ECL-0041178",
        "Type" : "ZSC",
        "version" : "00",
        "order" : "0530"
      } ]
    }
  }, {
    "order" : "0050",
    "text" : "307-005-BENCHING",
    "workCenter" : "358",
    "controlKey" : "SM01",
    "details" : {
      "longText" : "",
      "MICS" : [ ],
      "dependencies" : [ ],
      "documents" : [ {
        "Number" : "5B/ZSC-ECL-0041175",
        "Type" : "ZSC",
        "version" : "00",
        "order" : "0500"
      }, {
        "Number" : "5B/ZSC-ECL-0041176",
        "Type" : "ZSC",
        "version" : "00",
        "order" : "0510"
      }, {
        "Number" : "5B/ZSC-ECL-0041177",
        "Type" : "ZSC",
        "version" : "00",
        "order" : "0520"
      }, {
        "Number" : "5B/ZSC-ECL-0041178",
        "Type" : "ZSC",
        "version" : "00",
        "order" : "0530"
      } ]
    }
  }, {
    "order" : "0060",
    "text" : "220-001-IN PROCESS DIMENSIONAL INSP",
    "workCenter" : "358",
    "controlKey" : "ZQP1",
    "details" : {
      "longText" : "",
      "MICS" : [ {
        "order" : "0010",
        "MICNumber" : "C0027739",
        "Plant" : "ECL",
        "version" : "000001"
      } ],
      "dependencies" : [ ],
      "documents" : [ {
        "Number" : "5B/ZSC-ECL-0041175",
        "Type" : "ZSC",
        "version" : "00",
        "order" : "0500"
      }, {
        "Number" : "5B/ZSC-ECL-0041176",
        "Type" : "ZSC",
        "version" : "00",
        "order" : "0510"
      }, {
        "Number" : "5B/ZSC-ECL-0041177",
        "Type" : "ZSC",
        "version" : "00",
        "order" : "0520"
      }, {
        "Number" : "5B/ZSC-ECL-0041178",
        "Type" : "ZSC",
        "version" : "00",
        "order" : "0530"
      } ]
    }
  }, {
    "order" : "0070",
    "text" : "110-006-CHEMICAL CLEANING",
    "workCenter" : "048",
    "controlKey" : "ZQP1",
    "details" : {
      "longText" : "",
      "MICS" : [ {
        "order" : "0010",
        "MICNumber" : "C0006683",
        "Plant" : "ECL",
        "version" : "000001"
      } ],
      "dependencies" : [ ],
      "documents" : [ ]
    }
  } ]
}

请让我知道正确的方法,并随时发表您的想法。

2 个答案:

答案 0 :(得分:0)

ArrayList<operationsList> objOperationsList=new ArrayList<>();

class operationsList

{

String order;

String text;

String workCenter;

String controlKey;

Details objDetails;

}

class Details

{

   String longText;

ArrayList<MICS> objMICS=new ArrayList<>();

   ArrayList<Dependencies> objDependencies=new ArrayList<>();

   ArrayList<Documents> objDocuments=new ArrayList<>();

}

class MICS {}

class Dependencies {}

class Documents {}

答案 1 :(得分:0)

它的JSON列,而不是JSONB我在JSON列中有这个完整的JSON代码(粘贴在上面),我有操作表,其中列是order,workcenter,controlkey,表中的文本,所以我需要从JSON中选择值对象并将这些值插入到列中,以便我可以使用此查询

INSERT INTO public.operation(
 order_1, text, workcenter_number, control_key, long_text)
  (select 

router_payload :: json#&gt; '{operationsList,5}' - &gt;&gt; '订购', router_payload :: json#&gt; '{operationsList,5}' - &gt;&gt; '文本',

router_payload :: json#&gt; '{operationsList,5}' - &gt;&gt; '工作中心',

router_payload :: json#&gt; '{operationsList,5}' - &gt;&gt; 'CONTROLKEY', router_payload#&gt; '{operationsList,5}' - &gt; '详情' - &gt;&gt;'longText',

来自public.router_data的

);

但我的问题如果你在查询中看到我指的是{operation,5} 我不想让我尝试将其作为循环或者在执行查询时需要选择所有操作的东西