有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java为什么我的var返回不兼容问题出现错误13?

我试图在VBA中解析Json。 我正在从一个API收集数据,该API以字符串形式返回json格式。 我使用JsonConverter来解析我的字符串。 现在,当我想搜索它时,我得到了一个错误13不兼容类型

请参见下面的我的Java API:

@GetMapping("/rest/collectData/excel/exportAll")
public HashMap<Object, Object> collectAll(){
    HashMap<Object, Object> result = new HashMap<>();

    String sql = "SELECT affair_code AS codeAffair, name, amount, end_date AS state FROM service_record WHERE affair_code IS NOT NULL AND affair_code != ''";
    List<Map<String, Object>> allServiceRecords = jdbcTemplate.queryForList(sql);


    if(allServiceRecords != null && allServiceRecords.size() >0){
        result.put("result", true);
        for(Map<String, Object> serviceRecord : allServiceRecords){
            HashMap<Object, Object> details = new HashMap<>();
            if(result.containsKey(serviceRecord.get("codeAffair"))){
                details.put("alone", false);
                details.put("message", "Plusieurs prestations ont été trouvées.");
            } else {
                details.put("alone", true);
                details.put("name", (String) serviceRecord.get("name"));
                details.put("amount", (Double) serviceRecord.get("amount"));
                details.put("state", ((Date) serviceRecord.get("state")).compareTo(new Date()) < 0 ? "En cours" : "Clos");
            }
            result.put(serviceRecord.get("codeAffair"), details);
        }
    } else{
        result.put("result", false);
        result.put("error", "La liste n'est pas définie, ou vide.");
    }

    return result;

}

它返回json:

{"03-045251":{"alone":true,"amount":0.0,"name":"name1","state":"En cours"},"03_05494":{"alone":true,"amount":16743.0,"name":"name2","state":"En cours"}}

首先,我执行sql请求来收集数据并将其放入映射中。 然后,我将此地图发送到我的excel VBA

现在查看我的VBA:

Sub JsonDataSqwal()

firstRow = Range("A" & 11).End(xlDown).Row
lastRow = Range("A" & Rows.Count).End(xlUp).Row

Dim httpObject As Object
Set httpObject = CreateObject("MSXML2.XMLHTTP")


sUrl = "http://localhost/rest/collectData/excel/exportAll"

sRequest = sUrl
httpObject.Open "GET", sRequest, False
httpObject.send
sGetResult = httpObject.responseText

If Not IsNull(sGetResult) Then
    Dim oJson As Object
    JsonConverter.JsonOptions.AllowUnquotedKeys = True
    Set oJson = JsonConverter.ParseJson(sGetResult)
    
    Dim i As Long
    For i = firstRow To lastRow
        Dim codeAffString As String
        codeAffString = Cells(i, 4)
        Debug.Print oJson(codeAffString)("name")
    Next i
     
End If

端接头

目前,我试图打印我的数据。循环从一个列收集值,该列包含我的所有代码,如00_0000000-00000

正是这些数据,我试图用varcodeAffString在我的vba代码中使用。 当我执行我的代码时,我总是得到关于类型不兼容的错误13

为了解决这个问题,我尝试了很多方法:

  1. 将报价添加到我的var
  2. 将我的HashMap重命名为HashMap<String, Object>
  3. 允许取消引用键
  4. 更改我的后台程序
  5. 替换我的值,如"""" + codeAffairString + """"
  6. 用修复字符串"00_00000"替换我的var。它在这种情况下起作用
  7. 使用VarTyp函数检查我的var的类型,该函数为字符串索引返回8

现在我没有别的办法来解决我的问题。。 如果有人看到我的错误在哪里

谢谢大家!


共 (1) 个答案

  1. # 1 楼答案

    只是一个快速测试:

    我使用您提供的JSON字符串和您为codeAffString提供的值来构建minimal reproducible example,它不会产生任何错误:

    Sub test()
        Const JsonString As String = "{""03-045251"":{""alone"":true,""amount"":0.0,""name"":""name1"",""state"":""En cours""},""03_05494"":{""alone"":true,""amount"":16743.0,""name"":""name2"",""state"":""En cours""}}"
        Const codeAffString As String = "03-045251"
        
        Dim oJson As Object
        JsonConverter.JsonOptions.AllowUnquotedKeys = True
        Set oJson = JsonConverter.ParseJson(JsonString)
        
        Debug.Print oJson(codeAffString)("name")  ' outputs name1
    End Sub
    

    如果在JSON中找不到codeAffString,则会发生您描述的错误

    在代码中通过以下步骤进行测试:

    For i = firstRow To lastRow
        Dim codeAffString As String
        codeAffString = Cells(i, 4)
        If IsEmpty(oJson(codeAffString)) Then
            Debug.Print codeAffString & " does not exist in the json"
        Else
            Debug.Print oJson(codeAffString)("name")
        End If
    Next i