2017-02-13 4 views
1

私はアスタリスクpbxでluaを使用しています。 json文字列を処理しているときに次の問題が発生しました。luaを使用してjson文字列内でnull値を処理する方法は?

json "null"値がルアの関数型に変換されました。どうして?

このシナリオをどのように扱うか?なぜなら、jsonでは値がnullを意味し、nuaではluaでは何も意味しないため、私はnilを期待しているからです。うち上記のために入れ

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)) 

それはnull値を表現する方法を決定するために、特定のライブラリに任されて

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

です。 nilを使用すると、独自の問題が発生します。 元のJSONにはnull値のキーがあるか、そのようなキーがありません。 したがって、いくつかのライブラリはちょうどいくつかのユニークな値を返します。この値をjson.deconde(str, NULL_VALUE)のように渡す方法は、 です。 答えはちょうどあなたが使用しているライブラリのdoc/sourceを読んでいます。 ほとんどの場合、json.nullの値を確認すると のいずれかの値がNULLです。しかし、機能は本当に奇妙な選択です 彼らは一意のいくつかの未定義のルールを持っているので。 または別のライブラリをお試しください。

+0

json.util.nullは、最後に見つかった関数です。私は同じように処理することができます。あなたのポイントは、使用しているライブラリ/言語に依存しています。 – rajesh6115

関連する問題