搜尋
首頁Java使用放心java找不到路徑

使用放心java找不到路徑

Feb 14, 2024 am 10:51 AM

使用放心Java,找不到路徑?別擔心! php小編新一為您提供解決方案。在Java開發過程中,有時會遇到找不到指定路徑的問題,這可能是由於檔案路徑設定不正確或檔案不存在所致。本文將為您詳細介紹如何解決這個問題,並提供一些常見的解決方法。讓我們一起來探索吧!

問題內容

下面是我的程式碼,我嘗試設定為 json 格式的值:

{"details": "{\"user\":\"user1\",\"password\":\"1234\"}"}

在這裡,我必須在 user 和 pass 中設定數據,但它用雙引號引起來 ("")。

我嘗試了 detail.user 的路徑,但它不起作用:

ObjectMapper mapper = new ObjectMapper(); 
ObjectNode node = (ObjectNode) mapper.readTree(new File(templatePath)); 

// System.out.println(node); 

Configuration config = Configuration.builder()
    .jsonProvider(new JacksonJsonNodeJsonProvider())
    .mappingProvider(new JacksonMappingProvider()).build(); 
    
json = JsonPath.using(config).parse(node);

for (int i = 0; i < list.size(); i++) {
    String x = list.get(i); 
    arr = x.split(": "); 
    String newHeader = arr[0].replace("|", "."); 
    
    if (newHeader.contains("[")) { 
        String nHeader = "$." + newHeader; 
        String actualVal; 
        if (arr.length >= 2) { 
            actualVal = arr[1]; 
        } else { 
            actualVal = ""; 
        } 
        json.set(nHeader, actualVal).jsonString(); 
    } else { 
        String actualVal; 
        if (arr.length >= 2) { 
            actualVal = arr[1]; 
        } else { 
            actualVal = ""; 
        } 
        json.set(newHeader, actualVal).jsonString(); 
    }
}

我嘗試使用上面的程式碼來設定資料。但我收到 exception

解決方法

參考以下程式碼並嘗試更新您的物件。您可以使用 gson 或 jackson 來處理 json 物件。在發布問題之前,請先做一些工作。

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

public class JsonUpdateExample {

public static void main(String[] args) {
    // Sample JSON string
    String jsonString = "{\"name\":\"John\", \"age\":25, \"city\":\"New York\"}";

    // Field to update
    String fieldToUpdate = "age";
    
    // New value for the field
    int newValue = 30;

    // Update the JSON
    String updatedJson = updateJsonField(jsonString, fieldToUpdate, newValue);

    // Print the updated JSON
    System.out.println(updatedJson);
}

private static String updateJsonField(String jsonString, String fieldToUpdate, int newValue) {
    try {
        // Create ObjectMapper
        ObjectMapper objectMapper = new ObjectMapper();

        // Read the JSON string into a JsonNode
        JsonNode jsonNode = objectMapper.readTree(jsonString);

        // Update the field
        ((ObjectNode) jsonNode).put(fieldToUpdate, newValue);

        // Convert the updated JsonNode back to a JSON string
        return objectMapper.writeValueAsString(jsonNode);
    } catch (Exception e) {
        e.printStackTrace();
        return jsonString; // return the original JSON in case of an error
    }
}
}

以上是使用放心java找不到路徑的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述
本文轉載於:stackoverflow。如有侵權,請聯絡admin@php.cn刪除

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您聽不到任何人,如何修復音頻
3 週前By尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解鎖Myrise中的所有內容
3 週前By尊渡假赌尊渡假赌尊渡假赌

熱工具

Atom編輯器mac版下載

Atom編輯器mac版下載

最受歡迎的的開源編輯器

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

將Eclipse與SAP NetWeaver應用伺服器整合。

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

SecLists

SecLists

SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用