json 解析来自Unirest的响应并提取标记值

jgovgodb  于 2023-05-30  发布在  其他
关注(0)|答案(1)|浏览(148)

我使用Unireest在Java中调用一个REST端点。响应是一个JSON消息,它很复杂,内部包含多个级别。
我需要从响应消息中提取特定标记“eventDefinationId”的值。然而,这是失败或不起作用。我无法处理从响应中提取标记值的JSON解析
下面是我使用的代码

//*Enrich Journey Event Info *//
Statement stmt_eventId = conn.createStatement();
ResultSet rs_event = stmt_eventId.executeQuery("SELECT JourneyName,definitionid FROM journeydetails");
while(rs_event.next()){
    String JourneyName_For_EventId = rs_event.getString("JourneyName");
    JourneyName_For_EventId = JourneyName_For_EventId.replaceAll(" ", "%20");
    String Event_URL = "https://xxxxxxx.rest.marketingcloudapis.com/interaction/v1/interactions?name="+JourneyName_For_EventId+"&extras=all";
    Unirest.setTimeouts(0, 0);
    HttpResponse<String> response_event = Unirest.get(Event_URL)
                                            .header("Content-Type", "application/json")
                                            .header("Authorization", Token_Auth)
                                            .asString();
    String jsonString = response_event.getBody();
    ObjectMapper mapper = new ObjectMapper();  
    com.fasterxml.jackson.databind.JsonNode actualObj = mapper.readTree(jsonString);
    String eventDefinationId = actualObj.get("eventDefinitionId").textValue();
    System.out.println(eventDefinationId);
}

下面是使用Postman成功调用的示例。图像是相同的API调用,但使用Postman而不是代码。

正如你所看到的,eventDefinationId在下面(在元数据部分)。
我想把它提取出来。我得到了所有可能的错误类型。此外,该标签有时可能不存在。在这种情况下,它不应该出错,而是跳过并从结果集中转到下一个值。
当我运行上面的程序时,我得到空指针异常

Exception in thread "main" java.lang.NullPointerException
at sfmc_process.ProcessJourney.UpdateJourneyDetailsTables(ProcessJourney.java:105)
at sfmc_main.AppLauncher.main(AppLauncher.java:29)

命令执行失败。org.apache.commons.exec.ExecuteException:进程已退出,但出现错误:1(退出值:1)at org.apache.commons.exec.DefaultExecutor.executeInternal(DefaultExecutor.java:404)
第105行错误是:String eventDefinitionId = actualObj.get(“eventDefinitionId”).textValue();
下面是我正在使用的代码的图像。我只想从消息中提取eventDefinitionId。

xxe27gdn

xxe27gdn1#

谢谢大家的评论。在尝试和检查不同的网站后,我能够拿出解决方案或代码来解决问题
方法是,我以JSON的形式获得响应,然后我开始从中提取JSON对象和数组。如下图所示。谢谢大家

//*Enrich Journey Event Info *//
    Statement stmt_eventId = conn.createStatement();
    ResultSet rs_event = stmt_eventId.executeQuery("SELECT JourneyNames FROM journeynameedited");
    //ResultSet rs_event = stmt_eventId.executeQuery("SELECT JourneyName,definitionid FROM journeydetails");
    while(rs_event.next()){
        String JourneyName_For_EventId = rs_event.getString("JourneyNames").strip().replaceAll("\\p{C}", "");
        System.out.println(JourneyName_For_EventId);
        String JourneyDefinationId = "";
        //String JourneyDefinationId = rs_event.getString("definitionid");
        String JourneyName_For_EventId_html = JourneyName_For_EventId.replaceAll(" ", "%20");
        String Event_URL = "https://xxxxxxxxxx.rest.marketingcloudapis.com/interaction/v1/interactions?name="+JourneyName_For_EventId_html+"&extras=all";
        
        Unirest.setTimeouts(0, 0);
        HttpResponse<JsonNode> response_event = Unirest.get(Event_URL)
                                                .header("Content-Type", "application/json")
                                                .header("Authorization", Token_Auth)
                                                .asJson();
        JSONObject Response_Obj_Event = response_event.getBody().getObject();
        if(Response_Obj_Event.has("items"))
        {
            JSONArray Event_Items = Response_Obj_Event.getJSONArray("items");
            for(int ii=0;ii<Event_Items.length();ii++)
            {
                JSONObject Event_Object = Event_Items.getJSONObject(ii);
                if((Event_Object.has("triggers")) && ((Event_Object.getJSONArray("triggers")!= null) && (Event_Object.getJSONArray("triggers").length()>0 )))
                {
                    JSONArray Event_Trigger_Array = Event_Object.getJSONArray("triggers");
                    if((Event_Trigger_Array.getJSONObject(0).has("metaData")) && (Event_Trigger_Array.getJSONObject(0).getJSONObject("metaData")!= null))
                    {
                        if(Event_Trigger_Array.getJSONObject(0).getJSONObject("metaData").has("eventDefinitionId"))
                        {
                            String eventDefinitionId = Event_Trigger_Array.getJSONObject(0).getJSONObject("metaData").getString("eventDefinitionId");
                            String queryString = "INSERT INTO journeyeventinfo (JourneyName,DefinationId,EventId) VALUES (\""+JourneyName_For_EventId+"\",\""+JourneyDefinationId+"\",\""+eventDefinitionId+"\");";
                            populatejourneyEventTable(conn,queryString);
                        }                            
                    }
                }
            }
        }
    }

相关问题