Dart将JSON解析为表

时间:2014-07-02 13:16:42

标签: java javascript json http dart

我试图弄清楚如何将DSON Feed解析为Dart中的表格。我不确定是否应该使用地图,但如果我这样做,我不确定如何处理提取数据。我想让一个单元格保存assetID,然后另一个单元格保存域名,另一个单元格保存IP地址。如果您需要更多信息,请与我们联系。

落镖:

void loadData()
{
  var url = "http://localhost:8080/***selectAll";

  //call the web server asynchronously
  var request = HttpRequest.getString(url).then(onDataLoaded);

}



void onDataLoaded(String response)
{
  TableElement table = querySelector("#domainTableSelector");
  //table.children.add();

  var jsonString = response;
 // print(jsonString);

  List<String> list = new List<String>();
  list.add(jsonString);

  for (var x = 0; x < list.length; x++) 
  {
    //create new table row 
    TableRowElement row = table.insertRow(x+1);
    TableCellElement cell = row.insertCell(x);
    cell.text = list.toString();
     // print(list);
  }
//  Iterator itr= list.iterator();
//
//  print("Displaying List Elements,");
//
//   while(itr.hasNext())
//    {
//      print(itr.next());
//    }
}

JSON

[{"serviceResponseValue":[{"assetId":"8a41250446b89b5f0146b04d49910023","oplock":0,"longitude":115.86,"domainName":"free-aus-trip.au","latitude":-31.95,"ipAddress":"6.0.0.6"},{"assetId":"8a49859246918966014691b1aac9000c","oplock":0,"longitude":-65.30,"domainName":null,"latitude":-24.18,"ipAddress":"4.0.0.4"},{"assetId":"8a49859246876566014691b1437512e4","oplock":0,"longitude":77.60,"domainName":"allmovies.cn","latitude":12.97,"ipAddress":"14.0.0.14"},{"assetId":"8a49850446b04b5f0146b04d49910000","oplock":0,"longitude":112.47,"domainName":"getrichez.cn","latitude":32.98,"ipAddress":"5.0.0.5"},{"assetId":"8a498592469189660146919b7a210006","oplock":0,"longitude":-37.61,"domainName":"googles.com","latitude":55.75,"ipAddress":null},{"assetId":"8a42250876b89b5f0876b04d49910763","oplock":0,"longitude":-68.90,"domainName":"lolcatzfun.net","latitude":-22.48,"ipAddress":"8.0.0.8"},{"assetId":"8a498592469189660146919f8d700008","oplock":0,"longitude":113.50,"domainName":"ccn.com","latitude":52.03,"ipAddress":null},{"assetId":"8a45250446b89b5f0876b04d49910187","oplock":0,"longitude":115.84,"domainName":"free-aus-trip.au","latitude":-31.86,"ipAddress":"7.0.0.7"},{"assetId":"8a49859246918966014691aeda76000a","oplock":0,"longitude":3.38,"domainName":"cashnow.net","latitude":6.52,"ipAddress":"2.0.0.2"},{"assetId":"8a49859246918966014691ae19df0009","oplock":0,"longitude":7.48,"domainName":"free-money.tv","latitude":9.07,"ipAddress":"222.222.222.222"},{"assetId":"8a498592469189660146919e09900007","oplock":0,"longitude":30.34,"domainName":"facebok.com","latitude":59.93,"ipAddress":"111.111.111.222"},{"assetId":"8a49859246918966014691b14375000b","oplock":0,"longitude":116.41,"domainName":null,"latitude":39.90,"ipAddress":"0.0.0.111"}],"messages":{"messages":[]}}]

2 个答案:

答案 0 :(得分:2)

要使用JSON,你可以做一件好事:dart:convert => JSON

这里有一些使用示例:

var encoded = JSON.encode([1, 2, { "a": null }]);
var decoded = JSON.decode('["foo", { "bar": 499 }]');

我做了一些事情,但我不确定这是否完全适合你的需要

import 'dart:html';
import 'dart:convert';

void loadData()
{
  var url = "http://localhost:8080/***selectAll";

  //call the web server asynchronously
  var request = HttpRequest.getString(url).then(onDataLoaded);

}



void onDataLoaded(String response)
{
  TableElement table = querySelector("#domainTableSelector");
  //table.children.add();

  var jsonString = response;
 // print(jsonString);

  var jsonObject = JSON.decode(jsonString);

  for (var x = 0; x < jsonObject.length; x++) 
  {
    //create new table row 
    TableRowElement row = table.insertRow(x+1);

      for (var d in jsonObject[x]["serviceResponseValue"]) {
        TableCellElement cell = row.insertCell(x);
        cell.text = d["assetId"];

        cell = row.insertCell(x);
        cell.text = d["domainName"];

        cell = row.insertCell(x);
        cell.text = d["ipAddress"];

        print(d["assetId"]);
        print(d["domainName"]);
        print(d["ipAddress"]);

      }
     // print(list);
  }

}

答案 1 :(得分:0)

使用

将JSON解析为Dart数据结构
import 'dart:convert' show JSON;

var decoded = JSON.decode(jsonString);

另见How to handle JSON in Dart(亚历山大回答)

然后

decoded是Dart数据结构(列表,映射,值),您可以在调试器中迭代或调查。如果您需要进一步的帮助,请添加评论。