简体   繁体   中英

How to make a Javascript objects from given JSON data format

Hi I need help understanding how can I create objects from given JSON data:

   {
   "type":"FeatureCollection",
   "features":[

      {
         "type":"Feature",
         "properties":{
            "dbh":6
         },
         "geometry":{
            "type":"Point",
            "coordinates":[
               -79.96474,
               40.46283
            ]
         }
      },
      {
         "type":"Feature",
         "properties":{
            "dbh":2
         },
         "geometry":{
            "type":"Point",
            "coordinates":[
               -80.00949,
               40.42532
            ]
         }
      },
      {
         "type":"Feature",
         "properties":{
            "dbh":12
         },
         "geometry":{
            "type":"Point",
            "coordinates":[
               -79.93531,
               40.42282
            ]
         }
      }

I have done following:

let rsuCountsData = await axios.get('https://rsu-manager-apigateway 5xm3e3o5.uc.gateway.dev/rsucounts');
      this.createGeoJson(rsuCountsData.data); //calling the below function

I created the following function to get the value of count :

createGeoJson(data){
 //geojson={}
 //extract the count from data and store it in a object
 for (let [key, value] of Object.entries(data)) {
  console.log(key, value.count); //key= ip address

}

}

Now inside this function I want to create objects from that above JSON data but I am not sure how to do that because it looks too nested for me figure it out.Below is what I have done.It is mix of pseudo code and JS.Also don't think its correct but can someone help me point in correct direction how I can store these values in JS objects inside createGeoJson function:

     geojson = {}
      geojson.type = "FeatureCollection"
      geojson.features = []

   for key, val in data:
   feat.type = "Feature"
   props.count = val.count
    props.ipAddress = val.ipAddress// this is the key in above function.
   feat.properties = props
   geojson.features.append(feat)

Thanks and appreciate any input

I would suggest you to use JSON.parse() to convert JSON string to plain JS object.

After that you can iterate it with for...in , loops or any other iterator .

Generally, (after parsing your string via JSON.parse() ) you can loop over your Object.feature array with the .forEach function ( see MDN documentation ).

Not 100% sure what your exact output should be but this looks like a good use case for the .map() function ( MDN documentation here ). Say, you have your JSON stored in const json , you could do something like:

const json = { ... }
const newArray = json.features.map(el => Object.assign({},{type: el.type, properties: el.properties, geoType: el.geometry.type}));

newArray will be filled with Objects created from empty Objects and the Object.assign() function ( MDN documentation )

EDIT : You can also access the index within the .map() function for your counter.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM