简体   繁体   English

如何从给定的 JSON 数据格式制作 Javascript 对象

[英]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:嗨,我需要帮助了解如何从给定的 JSON 数据创建对象:

   {
   "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 :我创建了以下函数来获取 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:现在在这个函数中,我想从上面的 JSON 数据创建对象,但我不知道该怎么做,因为它看起来太嵌套了,我无法弄清楚。下面是我所做的。它是伪代码和 JS 的混合。也不要认为它是正确的,但有人可以帮助我指出正确的方向,我如何将这些值存储在 createGeoJson 函数内的 JS 对象中:

     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.我建议您使用JSON.parse()将 JSON 字符串转换为纯 JS 对象。

After that you can iterate it with for...in , loops or any other iterator .之后,您可以使用for...inloops或任何其他迭代器对其进行迭代

Generally, (after parsing your string via JSON.parse() ) you can loop over your Object.feature array with the .forEach function ( see MDN documentation ).通常,(通过JSON.parse()解析字符串后)您可以使用.forEach函数.forEach Object.feature数组( 请参阅 MDN 文档)。

Not 100% sure what your exact output should be but this looks like a good use case for the .map() function ( MDN documentation here ).不能 100% 确定您的确切输出应该是什么,但这看起来像是.map()函数的一个很好的用例( 此处为 MDN 文档)。 Say, you have your JSON stored in const json , you could do something like:假设您将 JSON 存储在const json ,您可以执行以下操作:

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 ) newArray将填充从空对象创建的对象和Object.assign()函数( MDN 文档

EDIT : You can also access the index within the .map() function for your counter.编辑:您还可以访问计数器的.map()函数中的索引。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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