2017-04-06 97 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

好吧!就是这样! :)感谢您的详细解答 –