json
  • lua
  • 2017-02-13 63 views 1 likes 
    1

    我在星號pbx中使用lua。處理json字符串時遇到以下問題。如何使用lua處理json字符串中的空值?

    json將空值轉換爲lua中的函數類型。爲什麼?

    如何處理這種情況?因爲我期待nil,因爲在json中沒有值意味着null,而零在lua中意味着什麼。

    local json = require("json") 
    local inspect = require("inspect") 
    local myjson_str='{"Sms":{"key":"xxxxxxxxxxxxxxxxxxxxx","to":"{caller}","senderid":null,"type":"Simple","content":"Your request has been accepted in Previous Miss call. We get back to you very soon."}}' 
    
    local myjson_table = json.decode(myjson_str) 
    print(type(myjson_table["Sms"]["senderid"])) 
    print(myjson_table) 
    print(inspect(myjson_table)) 
    print(json.encode(myjson_table)) 
    

    出把上面是

    function 
    table: 0xf5e770 
    { 
        Sms = { 
        content = "Your request has been accepted in Previous Miss call. We get back to you very soon.", 
        key = "xxxxxxxxxxxxxxxxxxxxx", 
        senderid = <function 1>, 
        to = "{caller}", 
        type = "Simple" 
        } 
    } 
    {"Sms":{"type":"Simple","key":"xxxxxxxxxxxxxxxxxxxxx","senderid":null,"content":"Your request has been accepted in Previous Miss call. We get back to you very soon.","to":"{caller}"}} 
    

    回答

    1

    它是由特定的庫來決定如何表述null值。 使用nil有它自己的問題,因爲它不可能找到 原始JSON具有空值的鍵或根本沒有這樣的鍵。 所以有些庫只是返回一些獨特的價值。有些提供 的方式來傳遞此值,如json.deconde(str, NULL_VALUE)。 所以回答只是閱讀你使用的圖書館的文檔/資源。 最有可能的是提供類似json.null的值來檢查 這兩個值中的任何一個爲空。但功能真的很奇怪,因爲他們有一些不確定的唯一性規則。 或嘗試另一個庫。

    +0

    json.util.null是我最終發現的函數。我設法處理它。你的觀點是正確的,它取決於你使用的庫/語言。 – rajesh6115

    相關問題