2016-01-20 62 views
-1

我想解析儿子数据波纹管。我的代码适用于简单的儿子文件,但不适用于此代码。我尝试了许多不同的组合来获取我想要的数据。我不想得到标题和董事会栖息地,你以粗体显示。解析特殊的Json数据Javascript

我真的不知道我能做什么。

我的数据和JavaScript代码。

function loadData() { 

    var text = '{ 
    "name": "", 
    "type": "", 
    "features": [{ 
     "type": "", 
     "geometry": { 
     "type": "", 
     "coordinates": [279947.135830549, 356999.268696635] 
     }, 
     "properties": { 
     "**title": "", 
     "timestamp": "", 
     "id": "", 
     "pos_acc": -1, 
     * * "Broad_Habi": "" * * , 
     "geom_X": 53.09665139, 
     "geom_Y": -3.79436993599, 
     "Image": "", 
     "Name": "", 
     "Species_Id": "", 
     "Approx_Hei": "", 
     "Evidence_o": "", 
     "Distance_f": "", 
     "River_Bank": "", 
     "Describe_T": "", 
     "Describe_W": "", 
     "Field_Comm": "", 
     "Make_and_M": "", 
     "Azimuth": 145.238262662, 
     "editor": "", 
     "Pitch": -25.5942265843, 
     "OS_Version": "", 
     "Roll": 12.322497107 1 
     } 
    }, { 
     "type": "", 
     "geometry": { 
     "type": "", 
     "coordinates": [280443.642983406, 358907.166228556] 
     }, 
     "properties": { 
     "title": "", 
     "timestamp": "", 
     "id": "", 
     "pos_acc": -1, 
     "Broad_Habi": "", 
     "geom_X": 53.1139046729, 
     "geom_Y": -3.78766989708, 
     "Image": "", 
     "Name": "", 
     "Species_Id": "", 
     "Approx_Hei": "", 
     "Evidence_o": "", 
     "Distance_f": "", 
     "River_Bank": "", 
     "Describe_T": "", 
     "Describe_W": "", 
     "Field_Comm": "", 
     "Make_and_M": "", 
     "Azimuth": 166.585854989, 
     "editor": "", 
     "Pitch": -48.7309271996, 
     "OS_Version": "", 
     "Roll": 8.1962176125 1 
     } 
    }] 
    }'; 
    obj = JSON.parse(text); 
    console.log(obj.features.length); 

    for (var i = 0; i < obj.properties.length; i++) { 
    var currentObj = obj.properties.[i]; 
    console.log(currentObj); 
    var mytitle = currentObj.title; 
    console.log(mytitle); 
    var habitat = currentObj.Broad_Habi; 
    console.log(habitat); 
    var table = document.getElementById("myTableData"); 

    var rowCount = table.rows.length; 
    var row = table.insertRow(rowCount); 
    row.insertCell(0).innerHTML = '<input type="button" value = "Delete" onClick="Javacsript:deleteRow(this)">'; 
    row.insertCell(1).innerHTML = mytitle; 
    row.insertCell(2).innerHTML = habitat; 
    } 
} 

回答

1

有你的JSON代码的多个错误:

  • 在此行中的明星:* * "Broad_Habi": "Rivers and streams" * * ,
  • 空间在你的号码:12.322497107 18.1962176125 1

我正确格式化它为你:

{ 
    "name": "SnowdoniaNationalParkJapaneseKnotweedSurvey2015_Cleaned", 
    "type": "FeatureCollection", 
    "features": [ 
    { 
     "type": "Feature", 
     "geometry": { 
     "type": "Point", 
     "coordinates": [ 
      279947.135830549, 
      356999.268696635 
     ] 
     }, 
     "properties": { 
     "**title": "Snowdonia National Park Japanese Knotweed Survey - point**", 
     "timestamp": "2015-07-07T10:32:48.460Z", 
     "id": "_x3ebvgy65", 
     "pos_acc": -1, 
     "Broad_Habi": "Rivers and streams", 
     "geom_X": 53.09665139, 
     "geom_Y": -3.79436993599, 
     "Image": "https://dyfi.cobwebproject.eu/pcapi/fs/local/2338e388-f34e-25d9-945c-54cffd9c46c2/records/LJ (6)/1436265141986.jpg", 
     "Name": "Expert", 
     "Species_Id": "Japanese Knotweed", 
     "Approx_Hei": "1m", 
     "Evidence_o": "No", 
     "Distance_f": "Very Close (<1m)", 
     "River_Bank": "No", 
     "Describe_T": "Warm", 
     "Describe_W": "Dry", 
     "Field_Comm": "Over 10m2", 
     "Make_and_M": "MOTOROLA XT1068", 
     "Azimuth": 145.238262662, 
     "editor": "b1b28830-9443-46b1-82f7-3d772f30cdbb.edtr", 
     "Pitch": -25.5942265843, 
     "OS_Version": "4.4.4", 
     "Roll": 12.3224971071 
     } 
    }, 
    { 
     "type": "Feature", 
     "geometry": { 
     "type": "Point", 
     "coordinates": [ 
      280443.642983406, 
      358907.166228556 
     ] 
     }, 
     "properties": { 
     "title": "Snowdonia National Park Japanese Knotweed Survey - point", 
     "timestamp": "2015-07- 07T13:06:48.394Z", 
     "id": "_vz9f5kbbe", 
     "pos_acc": -1, 
     "Broad_Habi": "Rivers and streams", 
     "geom_X": 53.1139046729, 
     "geom_Y": -3.78766989708, 
     "Image": "https://dyfi.cobw ebproject.eu/pcapi/fs/local/2338e388-f34e-25d9-945c-54cffd9c46c2/records/Doug (16)/1436274370609.jpg", 
     "Name": "Expert", 
     "Species_Id": "Japanese Knotweed", 
     "Approx_Hei": "2m", 
     "Evidence_o": "No", 
     "Distance_f": "Far (3m- 10m)", 
     "River_Bank": "Yes", 
     "Describe_T": "Warm", 
     "Describe_W": "Dry", 
     "Field_Comm": "Far side of river. Visible through the branches in the picture", 
     "Make_and_M": "SONY D6603", 
     "Azimuth": 166.585854989, 
     "editor": "b1b28830-9443-46b1-82f7- 3d772f30cdbb.edtr", 
     "Pitch": -48.7309271996, 
     "OS_Version": "5.0.2", 
     "Roll": 8.19621761251 
     } 
    } 
    ] 
} 

通过功能的代码循环将是:

var obj = JSON.parse(text); 
for(var i = 0; i < obj.features.length; i++) { 
    var featureTitle = obj.features[i].properties.title; 
    var featureHab = obj.features[i].properties.Broad_Habi; 
} 
+0

**星星是唯一的堆栈溢出的页面,是不是在我的数据的情况下上。只是因为我想指出我想得到的数据。你知道我必须改变我的JavaScript代码才能获得标题和Board Habitat吗? – Ben