【问题标题】:Problem Nested JSON cant able to show value in datatable问题嵌套 JSON 无法在数据表中显示值
【发布时间】:2019-02-06 13:02:35
【问题描述】:

来自服务器的响应:

["{\"CLIENT\":[{\"tranche\":\"1-4\",\"prix\":\"65.96\",\"currency\":\"E\"}],\"DISTRIBUTEUR\":[{\"tranche\":\"1-4\",\"prix\":52.77,\"currency\":\"E\"}],\"FILLIALE\":[{\"tranche\":\"1-4\",\"prix\":46.17,\"currency\":\"E\"}],\"NULL\":[{\"error\":\"Compte inconnu\"}],\"NCN_23\":[{\"prix\":46.17,\"currency\":0}],\"NNA_USA\":[{\"prix\":46.17,\"currency\":0}]}","{\"CLIENT\":[{\"tranche\":\"5-9\",\"prix\":\"57.38\",\"currency\":\"E\"}],\"DISTRIBUTEUR\":[{\"tranche\":\"5-9\",\"prix\":45.9,\"currency\":\"E\"}],\"FILLIALE\":[{\"tranche\":\"5-9\",\"prix\":40.17,\"currency\":\"E\"}],\"NULL\":[{\"error\":\"Compte inconnu\"}],\"NCN_23\":[{\"prix\":40.17,\"currency\":0}],\"NNA_USA\":[{\"prix\":40.17,\"currency\":0}]}","{\"CLIENT\":[{\"tranche\":\"10-24\",\"prix\":\"53.61\",\"currency\":\"E\"}],\"DISTRIBUTEUR\":[{\"tranche\":\"10-24\",\"prix\":42.89,\"currency\":\"E\"}],\"FILLIALE\":[{\"tranche\":\"10-24\",\"prix\":37.53,\"currency\":\"E\"}],\"NULL\":[{\"error\":\"Compte inconnu\"}],\"NCN_23\":[{\"prix\":37.53,\"currency\":0}],\"NNA_USA\":[{\"prix\":37.53,\"currency\":0}]}","{\"CLIENT\":[{\"tranche\":\"25-49\",\"prix\":\"46.49\",\"currency\":\"E\"}],\"DISTRIBUTEUR\":[{\"tranche\":\"25-49\",\"prix\":37.19,\"currency\":\"E\"}],\"FILLIALE\":[{\"tranche\":\"25-49\",\"prix\":32.54,\"currency\":\"E\"}],\"NULL\":[{\"error\":\"Compte inconnu\"}],\"NCN_23\":[{\"prix\":32.54,\"currency\":0}],\"NNA_USA\":[{\"prix\":32.54,\"currency\":0}]}","{\"CLIENT\":[{\"tranche\":\"50-99\",\"prix\":\"45.55\",\"currency\":\"E\"}],\"DISTRIBUTEUR\":[{\"tranche\":\"50-99\",\"prix\":36.44,\"currency\":\"E\"}],\"FILLIALE\":[{\"tranche\":\"50-99\",\"prix\":31.89,\"currency\":\"E\"}],\"NULL\":[{\"error\":\"Compte inconnu\"}],\"NCN_23\":[{\"prix\":31.89,\"currency\":0}],\"NNA_USA\":[{\"prix\":31.89,\"currency\":0}]}","{\"CLIENT\":[{\"tranche\":\"100-249\",\"prix\":\"37.75\",\"currency\":\"E\"}],\"DISTRIBUTEUR\":[{\"tranche\":\"100-249\",\"prix\":30.2,\"currency\":\"E\"}],\"FILLIALE\":[{\"tranche\":\"100-249\",\"prix\":26.43,\"currency\":\"E\"}],\"NULL\":[{\"error\":\"Compte inconnu\"}],\"NCN_23\":[{\"prix\":26.43,\"currency\":0}],\"NNA_USA\":[{\"prix\":26.43,\"currency\":0}]}","{\"CLIENT\":[{\"tranche\":\"250-499\",\"prix\":\"33.14\",\"currency\":\"E\"}],\"DISTRIBUTEUR\":[{\"tranche\":\"250-499\",\"prix\":26.51,\"currency\":\"E\"}],\"FILLIALE\":[{\"tranche\":\"250-499\",\"prix\":23.2,\"currency\":\"E\"}],\"NULL\":[{\"error\":\"Compte inconnu\"}],\"NCN_23\":[{\"prix\":23.2,\"currency\":0}],\"NNA_USA\":[{\"prix\":23.2,\"currency\":0}]}","{\"CLIENT\":[{\"tranche\":\"500-999\",\"prix\":\"29.31\",\"currency\":\"E\"}],\"DISTRIBUTEUR\":[{\"tranche\":\"500-999\",\"prix\":23.45,\"currency\":\"E\"}],\"FILLIALE\":[{\"tranche\":\"500-999\",\"prix\":20.52,\"currency\":\"E\"}],\"NULL\":[{\"error\":\"Compte inconnu\"}],\"NCN_23\":[{\"prix\":20.52,\"currency\":0}],\"NNA_USA\":[{\"prix\":20.52,\"currency\":0}]}","{\"CLIENT\":[{\"tranche\":\"1000+\",\"prix\":\"27.95\",\"currency\":\"E\"}],\"DISTRIBUTEUR\":[{\"tranche\":\"1000+\",\"prix\":22.36,\"currency\":\"E\"}],\"FILLIALE\":[{\"tranche\":\"1000+\",\"prix\":19.57,\"currency\":\"E\"}],\"NULL\":[{\"error\":\"Compte inconnu\"}],\"NCN_23\":[{\"prix\":19.57,\"currency\":0}],\"NNA_USA\":[{\"prix\":19.57,\"currency\":0}]}"]

     var jsonData=JSON.parse(response.getReturnValue());
                        console.log(jsonData); 

  component.set('v.mycolumns',[{label: 'S No', fieldName: 'SNO', type: 'text'},
                     {label: 'Client Type', fieldName: 'Client', type: 'text'},
                     {label: 'Slab', fieldName: 'tranche', type: 'text'},
                     {label: 'Price', fieldName: 'prix', type: 'text'},
                   {label: 'Currency', fieldName: 'currency', type: 'text'},

                         ])

                       var rows=[];              
                            for (var key in jsonData){
                             var curName = key;
                             var value = jsonData[key];
                             var test = jsonData[key[value]];
                       rows.push({SNO : curName,
                       Client:jsonData[curName]+"",
                       currency:(jsonData[curName[test]].currency)+"",  
                       prix:(jsonData[curName[test]].prix)+"",
                       tranche:(jsonData[curName[test]].tranche)+""});   
                       }
                        component.set('v.mydata',rows);  
                    }
              });
                // enqueue the server side action  
                $A.enqueueAction(action);

我想在我的数据表中显示这个项目

我将所有值都设为“未定义”

有人可以帮我解决这个错误吗?

【问题讨论】:

  • getReturnValue 返回什么?那是第 1 行吗?
  • @82Tuskers 以上 json 在我的第一行中提到是
  • ..但这是一个包含元素 [0] 处的字符串的数组。

标签: javascript jquery angularjs json


【解决方案1】:

你可以试试吗

JSON.parse(response.getReturnValue()[0])

我认为这应该适用于上述 JSON 响应。 JSON 是有效的。

【讨论】:

  • 它只给出结果“[”作为上述代码的输出
  • 好的,知道了......然后你想做JSON.parse(response.getReturnValue())[0]并将其分配给jsonData
  • 不,它也有效。它只返回 1 组。我有 8 组数据
  • 这可能是服务器响应问题。因为在粘贴的 JSON 中,我只看到以下键的一个数组元素:[ "CLIENT", "DISTRIBUTEUR", "FILLIALE", "NULL", "NCN_23", "NNA_USA" ]
【解决方案2】:

试试这个:

    var myMessage=["{\"CLIENT\":[{\"tranche\":\"1-4\",\"prix\":\"65.96\",\"currency\":\"E\"}],\"DISTRIBUTEUR\":[{\"tranche\":\"1-4\",\"prix\":52.77,\"currency\":\"E\"}],\"FILLIALE\":[{\"tranche\":\"1-4\",\"prix\":46.17,\"currency\":\"E\"}],\"NULL\":[{\"error\":\"Compte inconnu\"}],\"NCN_23\":[{\"prix\":46.17,\"currency\":0}],\"NNA_USA\":[{\"prix\":46.17,\"currency\":0}]}","{\"CLIENT\":[{\"tranche\":\"5-9\",\"prix\":\"57.38\",\"currency\":\"E\"}],\"DISTRIBUTEUR\":[{\"tranche\":\"5-9\",\"prix\":45.9,\"currency\":\"E\"}],\"FILLIALE\":[{\"tranche\":\"5-9\",\"prix\":40.17,\"currency\":\"E\"}],\"NULL\":[{\"error\":\"Compte inconnu\"}],\"NCN_23\":[{\"prix\":40.17,\"currency\":0}],\"NNA_USA\":[{\"prix\":40.17,\"currency\":0}]}","{\"CLIENT\":[{\"tranche\":\"10-24\",\"prix\":\"53.61\",\"currency\":\"E\"}],\"DISTRIBUTEUR\":[{\"tranche\":\"10-24\",\"prix\":42.89,\"currency\":\"E\"}],\"FILLIALE\":[{\"tranche\":\"10-24\",\"prix\":37.53,\"currency\":\"E\"}],\"NULL\":[{\"error\":\"Compte inconnu\"}],\"NCN_23\":[{\"prix\":37.53,\"currency\":0}],\"NNA_USA\":[{\"prix\":37.53,\"currency\":0}]}","{\"CLIENT\":[{\"tranche\":\"25-49\",\"prix\":\"46.49\",\"currency\":\"E\"}],\"DISTRIBUTEUR\":[{\"tranche\":\"25-49\",\"prix\":37.19,\"currency\":\"E\"}],\"FILLIALE\":[{\"tranche\":\"25-49\",\"prix\":32.54,\"currency\":\"E\"}],\"NULL\":[{\"error\":\"Compte inconnu\"}],\"NCN_23\":[{\"prix\":32.54,\"currency\":0}],\"NNA_USA\":[{\"prix\":32.54,\"currency\":0}]}","{\"CLIENT\":[{\"tranche\":\"50-99\",\"prix\":\"45.55\",\"currency\":\"E\"}],\"DISTRIBUTEUR\":[{\"tranche\":\"50-99\",\"prix\":36.44,\"currency\":\"E\"}],\"FILLIALE\":[{\"tranche\":\"50-99\",\"prix\":31.89,\"currency\":\"E\"}],\"NULL\":[{\"error\":\"Compte inconnu\"}],\"NCN_23\":[{\"prix\":31.89,\"currency\":0}],\"NNA_USA\":[{\"prix\":31.89,\"currency\":0}]}","{\"CLIENT\":[{\"tranche\":\"100-249\",\"prix\":\"37.75\",\"currency\":\"E\"}],\"DISTRIBUTEUR\":[{\"tranche\":\"100-249\",\"prix\":30.2,\"currency\":\"E\"}],\"FILLIALE\":[{\"tranche\":\"100-249\",\"prix\":26.43,\"currency\":\"E\"}],\"NULL\":[{\"error\":\"Compte inconnu\"}],\"NCN_23\":[{\"prix\":26.43,\"currency\":0}],\"NNA_USA\":[{\"prix\":26.43,\"currency\":0}]}","{\"CLIENT\":[{\"tranche\":\"250-499\",\"prix\":\"33.14\",\"currency\":\"E\"}],\"DISTRIBUTEUR\":[{\"tranche\":\"250-499\",\"prix\":26.51,\"currency\":\"E\"}],\"FILLIALE\":[{\"tranche\":\"250-499\",\"prix\":23.2,\"currency\":\"E\"}],\"NULL\":[{\"error\":\"Compte inconnu\"}],\"NCN_23\":[{\"prix\":23.2,\"currency\":0}],\"NNA_USA\":[{\"prix\":23.2,\"currency\":0}]}","{\"CLIENT\":[{\"tranche\":\"500-999\",\"prix\":\"29.31\",\"currency\":\"E\"}],\"DISTRIBUTEUR\":[{\"tranche\":\"500-999\",\"prix\":23.45,\"currency\":\"E\"}],\"FILLIALE\":[{\"tranche\":\"500-999\",\"prix\":20.52,\"currency\":\"E\"}],\"NULL\":[{\"error\":\"Compte inconnu\"}],\"NCN_23\":[{\"prix\":20.52,\"currency\":0}],\"NNA_USA\":[{\"prix\":20.52,\"currency\":0}]}","{\"CLIENT\":[{\"tranche\":\"1000+\",\"prix\":\"27.95\",\"currency\":\"E\"}],\"DISTRIBUTEUR\":[{\"tranche\":\"1000+\",\"prix\":22.36,\"currency\":\"E\"}],\"FILLIALE\":[{\"tranche\":\"1000+\",\"prix\":19.57,\"currency\":\"E\"}],\"NULL\":[{\"error\":\"Compte inconnu\"}],\"NCN_23\":[{\"prix\":19.57,\"currency\":0}],\"NNA_USA\":[{\"prix\":19.57,\"currency\":0}]}"];

    var jsonData = JSON.parse("["+myMessage+"]");
    
    console.log(jsonData);

【讨论】:

    猜你喜欢
    • 2019-01-06
    • 2015-12-21
    • 1970-01-01
    • 2018-07-05
    • 2021-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多