How to parse JSON with VBA without external libraries?

前端 未结 3 595
囚心锁ツ
囚心锁ツ 2020-11-30 04:15

I have a json like below:

{\"sentences\":[{\"trans\":\"something ru\",\"orig\":\"english word\",\"translit\":\"Angliyskoye slovo\",\"src_translit\":\"\"}], \         


        
3条回答
  •  情话喂你
    2020-11-30 04:45

    There are two issues here. The first is to access fields in the array returned by your JSON parse, the second is to rename collections/fields (like sentences) away from VBA reserved names.

    Let's address the second concern first. You were on the right track. First, replace all instances of sentences with jsentences If text within your JSON also contains the word sentences, then figure out a way to make the replacement unique, such as using "sentences":[ as the search string. You can use the VBA Replace method to do this.

    Once that's done, so VBA will stop renaming sentences to Sentences, it's just a matter of accessing the array like so:

    'first, declare the variables you need:
    Dim jsent as Variant
    
    'Get arr all setup, then
    For Each jsent in arr.jsentences
      MsgBox(jsent.orig)
    Next
    

提交回复
热议问题