為數據交換定義JSON結構,支持表和列屬性早上好,
我正在研究標準數據交換結構,以允許JMP和我們的其他應用程序交換數據。 JMP的本機JSON功能可以很好地處理數據,但不提供保存屬性的選項。 我想知道是否有人已經完成了這個練習,並願意分享給你想出來的?
謝謝
Ray Bunkofske
Re:為數據交換定義JSON結構,支持表和列屬性在我的情況下,我有一個Associateve數組,其中包含值和其他AA和嵌套在其中的列表,我需要將其發送到JSON文件中的另一個系統。 如果你可以將你想要的屬性作為List或AA獲得,你可以將它傳遞給這個函數,或者你可以調整它來迭代其他結構。
Re:為數據交換定義JSON結構,支持表和列屬性安德魯,
謝謝,我非常感謝這裡所付出的努力。 要使用它,我是否傳入單個元素並以JSON結構返回它們?
再次感謝
Re:為數據交換定義JSON結構,支持表和列屬性已經發現了一個問題;我試圖迭代列表,就像它們是關聯數組一樣。這應該更好。// Serialize Associative Arrays, Lists, or nestings of them for JSON export (http://www.json.org/)
// Iterate through Associative Array keys or List, recurse on values.
// Use "Local" and "Recurse" to isolates scopes.
// https://www.jmp.com/support/help/13-2/Recursion.shtml
// http://www.jmp.com/support/help/Global_and_Local_Variables.shtml
// TODO: consider pretty formatting
// TODO: consider opiton to include/exclude empty Lists and AAs.
toJSON = Function( {input, depth=0, maxdepth=16},
Local( {output, current_item, num_list_items, list_item_count},
if (
depth > maxdepth,
Throw("toJSON recursion depth " || Char(depth) || " exceeds limit of " || Char(maxdepth)),
Is List(input),
output = "[";
num_list_items = N Items(input);
list_item_count = 0;
While (num_list_items > list_item_count,
list_item_count++;
output ||= Recurse(input[list_item_count], depth+1);
If (num_list_items > list_item_count, output ||= ",";);
);
output ||= "]",
Is Associative Array(input),
output = "{";
current_item = input << First;
// TODO: check value for Is Missing before adding key
While (Is Empty(current_item) == 0,
output ||= "\!"" || Char(current_item) || "\!":" || Recurse(input[current_item], depth+1);
current_item = input << Next(current_item);
If (Is Empty(current_item) == 0, output ||= ",";);
);
output ||= "}",
Is Number(input),
output = Char(input),
Uppercase(input) == "FALSE",
output = "false",
Uppercase(input) == "TRUE",
output = "true",
// else
output = "\!"" || Char(input) || "\!"",
);
// return:
output;
);
);Re:為數據交換定義JSON結構,支持表和列屬性嗨雷,
我把它們放在一起,以便將一些數據保存到JSON中。沒有保證,但歡迎你試一試。
乾杯
-安德魯
// Serialize Associative Arrays, Lists, or nestings of them to JSON (http://www.json.org/)
// Iterate through Associative Array keys or List, recurse on values.
// Use "Local" and "Recurse" to isolates scopes.
// https://www.jmp.com/support/help/13-2/Recursion.shtml
// http://www.jmp.com/support/help/Global_and_Local_Variables.shtml
toJSON = Function( {input, depth=0, maxdepth=16},
Local( {output, current_item},
if (
depth > maxdepth,
Throw("toJSON recursion depth " || depth || " exceeds limit of " || maxdepth),
Is List(input),
output = "[";
current_item = input << First;
While (Is Empty(current_item) == 0,
output = output || Recurse(current_item, depth+1);
current_item = input << Next(current_item);
If (Is Empty(current_item) == 0, output = output || ",";);
);
output = output || "]",
Is Associative Array(input),
output = "{";
current_item = input << First;
While (Is Empty(current_item) == 0,
output = output || "\!"" || Char(current_item) || "\!":" || Recurse(input[current_item], depth+1);
current_item = input << Next(current_item);
If (Is Empty(current_item) == 0, output = output || ",";);
);
output = output || "}",
Is Number(input),
output = Char(input),
Uppercase(input) == "FALSE",
output = "false",
Uppercase(input) == "TRUE",
output = "true",
// else
output = "\!"" || Char(input) || "\!"",
);
// return:
output;
);
);