2017-04-06 62 views
2

我從中國的AQI API獲取數據,我將一些數據放入SQL服務器數據庫,並且工作得很花哨。我還決定將從API返回的所有內容添加到JSON文件中,以便稍後訪問它。用雙引號將字符串添加到vba中的文件中

每當我嘗試添加該API返回到該文件中,我得到一個「無效的過程調用或參數」字符串錯誤

這是我迄今所做的:

Public Sub AddToJson(Jsonline As String) 
    Dim strfile As String 
    Dim fso As New FileSystemObject 
    Dim fsoStream As TextStream 
    Dim iexist As String 
    Dim stradd As String 

    strfile = "c:\JSON_AQI.json" 
    stradd = Replace(Jsonline, Chr(34), Chr(34) & Chr(34) & Chr(34) & Chr(34)) 
    Debug.Print stradd 
    iexist = Dir(strfile) 

    'check if the file exists 
    If iexist = "" Then 
     'if it exists, open it and add the line 
     Set fsoStream = fso.CreateTextFile(strfile) 
    Else 
     'if it doesn't exist, create it and add the line 
     Set fsoStream = fso.OpenTextFile(strfile, ForAppending) 
    End If 

    fsoStream.WriteLine stradd 

    fsoStream.Close 

    Set fsoStream = Nothing 
    Set fso = Nothing 


End Sub 

這就是我傳的Jsonline參數:

{"status":"ok","data":{"aqi":164,"idx":7130,"attributions":[{"name":"Hunan Environmental Protection Agency (????????)"},{"name":"China National Urban air quality real-time publishing platform (??????????????)"}],"city":{"geo":[33.8561,115.7831],"name":"sanguó lanshèng gong, Bozhou"},"dominentpol":"pm25","iaqi":{"co":{"v":14.8},"no2":{"v":24.7},"o3":{"v":45.9},"pm10":{"v":97},"pm25":{"v":164},"so2":{"v":5.1}},"time":{"s":"2017-04-06 04:00:00","tz":"+08:00","v":1491451200}}} 

,你可以看到我嘗試添加額外的雙引號字符串無濟於事,還有什麼我失蹤?

+1

爲什麼你需要翻兩番報價? – omegastripes

+0

我使用它們作爲轉義字符,如下所示:http://stackoverflow.com/questions/9024724/how-do-i-put-double-quotes-in-a-string-in-vba –

+0

您需要轉義引號如果您只在VBA編輯器中編寫字符串常量,則採用這種方式。請注意,該答案中有雙引號,而不是四位。在你的情況下,字符串值已經保存在'Jsonline'變量中,所以不需要轉義。 – omegastripes

回答

0

其實收到的JSON包含中國字符,而不是?你貼,應該是這樣的:

{ 
    "status": "ok", 
    "data": { 
     "aqi": 164, 
     "idx": 7130, 
     "attributions": [ 
      { 
       "name": "Hunan Environmental Protection Agency (湖南省環境保護廳)" 
      }, 
      { 
       "name": "China National Urban air quality real-time publishing platform (全國城市空氣質量實時發佈平臺)" 
      } 
     ], 
     "city": { 
      "geo": [ 
       33.8561, 
       115.7831 
      ], 
      "name": "sanguó lanshèng gong, Bozhou" 
     }, 
     "dominentpol": "pm25", 
     "iaqi": { 
      "co": { 
       "v": 14.8 
      }, 
      "no2": { 
       "v": 24.7 
      }, 
      "o3": { 
       "v": 45.9 
      }, 
      "pm10": { 
       "v": 97 
      }, 
      "pm25": { 
       "v": 164 
      }, 
      "so2": { 
       "v": 5.1 
      } 
     }, 
     "time": { 
      "s": "2017-04-06 04:00:00", 
      "tz": "+08:00", 
      "v": 1491451200 
     } 
    } 
} 

因此,你必須設置片斷的編碼被追加爲Unicode明確。我建議使用下面的簡單功能通過Scripting.FileSystemObject讀,寫和追加的文本文件:

Function ReadTextFile(sPath As String, lFormat As Long) As String 
    ' lFormat -2 - System default, -1 - Unicode, 0 - ASCII 
    With CreateObject("Scripting.FileSystemObject").OpenTextFile(sPath, 1, False, lFormat) 
     ReadTextFile = "" 
     If Not .AtEndOfStream Then ReadTextFile = .ReadAll 
     .Close 
    End With 
End Function 

Sub WriteTextFile(sContent As String, sPath As String, lFormat As Long) 
    With CreateObject("Scripting.FileSystemObject").OpenTextFile(sPath, 2, True, lFormat) 
     .Write sContent 
     .Close 
    End With 
End Sub 

Sub AppendTextFile(sContent As String, sPath As String, lFormat As Long) 
    With CreateObject("Scripting.FileSystemObject").OpenTextFile(sPath, 8, True, lFormat) 
     .Write sContent 
     .Close 
    End With 
End Sub 

所以,你可以通過調用追加字符串:

AppendTextFile Jsonline & vbCrLf, "C:\JSON_AQI.json", -1 
+0

好吧!就是這樣! :)感謝您的詳細解答 –