使用JSON.stringify但数据仍然有数组,这是什么意思?

时间:2013-05-14 17:58:06

标签: json

我有一个对象数组的数据。它包含树结构中的对象数组。我使用JSON.stringify(myArray),但数据仍然包含数组,因为我在转换后的数据中看到了[]。

就我而言,我希望将所有数据转换为json对象而不是数组,因为我需要使用SAPUI5的TreeTable上的数据。

也许我误解了。请帮我清楚。

这是我从JSON.stringify获得的数据示例。

[{"value":{"Id":"00145E5BB2641EE284F811A7907717A3",
"Text":"BI-RA                   Reporting, analysis, and dashboards",
"Parent":"00145E5BB2641EE284F811A79076F7A3","Type":"BMF"},
"children":[{"value":{"Id":"00145E5BB2641EE284F811A7907737A3",
"Text":"WebIntelligence_4.1","Parent":"00145E5BB2641EE284F811A7907717A3",
"Type":"TWB"},"children":[{"value":{"Id":"00145E5BB2641EE284F811A7907757A3",
"Text":"Functional Areas","Parent":"00145E5BB2641EE284F811A7907737A3","Type":"TWB"},
"children":[{"value":{"Id":"00145E5BB2641EE284F811A7907777A3",
"Text":"CHARTING","Parent":"00145E5BB2641EE284F811A7907757A3","Type":"TWB"},
"children":[{"value":{"Id":"001999E0B9081EE28AB706BE26631E93",
"Text":"Drill","Parent":"00145E5BB2641EE284F811A7907777A3","Type":"TWB"},
"children":[{"value":{"Id":"001999E0B9081EE28AB706BE26633E93",
"Text":"[AUTO][ACCEPT] Drill on charts DHTML","Parent":"001999E0B9081EE28AB706BE26631E93",
"Type":"TWB","Ref":"UT_WEBI_CHARTS_DRILL_HTML"}},{"value":{"Id":"001999E0B9081EE28AB706BE26635E93",
"Text":"[AUTO][ACCEPT] Drill on charts JAVA","Parent":"001999E0B9081EE28AB706BE26631E93",
"Type":"TWB","Ref":"UT_WEBI_CHARTS_DRILL_JAVA"}}]},...

我想要的输出不应该是对象的数组,但应该是......

   {{"value":{
        "Id":"00145E5BB2641EE284F811A7907717A3",
        "Text":"BI-RA                   Reporting, analysis, and dashboards",
        "Parent":"00145E5BB2641EE284F811A79076F7A3","Type":"BMF"},
        "children":{
           {"value":{
                "Id":"00145E5BB2641EE284F811A7907737A3",
                "Text":"WebIntelligence_4.1",
                "Parent":"00145E5BB2641EE284F811A7907717A3",
                "Type":"TWB"},
                "children":{
                      {"value":{
                          "Id":"00145E5BB2641EE284F811A7907757A3",
                          "Text":"Functional Areas",
                          "Parent":"00145E5BB2641EE284F811A7907737A3",
                          "Type":"TWB"},...

2 个答案:

答案 0 :(得分:1)

JSON.stringify仅将JavaScript数据结构转换为JSON格式的字符串,供其他解析器(包括JSON.parse)使用。如果要将其字符串化为其他值,则必须首先更改源数据结构。

但是,似乎这不能表示为数组以外的任何内容,因为您有重复的键(即value出现多次)。这对于JavaScript对象或此类的JSON表示无效。

答案 1 :(得分:0)

我认为你想要的是

JSON.stringify(data[0]);

或者

JSON.stringify(data[0].value);

其中data是您在问题中传递的对象