将网址url中的参数转化为JSON格式的两种方法

在我们进入主题前,我先先看下获取网址URL的方法:

window.location.href // 设置或获取整个URL为字符串 

window.location.hash // 设置或获取href属性中在井号#后面的部分参数 

window.location.search // 设置或获取href属性中跟在问号?后面,井号#前面的部分参数

例如我们这里有一个url,例如:http://127.0.0.1:8080/html/urltojson.html?id=1&name=good#&price=1003

下面看下上面三个方法是如何使用的

console.log(window.location.href);
// http://127.0.0.1:8080/html/urltojson.html?id=1&name=good#&price=1003
console.log(window.location.hash);
// #&price=1003
console.log(window.location.search);
// ?id=1&name=good

 

我们看到了上面三个方法的返回参数是不一样的,我们接下来看下如果将url转换为json格式的数据。

第一种: for 循环方式

// 第一种: for循环
var GetQueryJson1 = function () {
  let url = location.href; // 获取当前浏览器的URL
  let arr = []; // 存储参数的数组
  let res = {}; // 存储最终JSON结果对象
  arr = url.split('?')[1].split('&'); // 获取浏览器地址栏中的参数
  
  for (let i = 0; i < arr.length; i++) { // 遍历参数
    if (arr[i].indexOf('=') != -1){ // 如果参数中有值
      let str = arr[i].split('=');
      res[str[0]] = str[1];
    } else { // 如果参数中无值
      res[arr[i]] = '';
    }
  }
  return res;
}
console.log(GetQueryJson1());

 

第二种:正则表达式方式

// 第二种:正则表达式
var GetQueryJson2 = function () {
  let url = location.href; // 获取当前浏览器的URL
  let param = {}; // 存储最终JSON结果对象
  url.replace(/([^?&]+)=([^?&]+)/g, function(s, v, k) {
    param[v] = decodeURIComponent(k);//解析字符为中文
    return k + '=' +  v;
  });
  return param;
}

console.log(GetQueryJson2());

 

以上所述是小端给大家介绍的JS将网址url转化为JSON格式的方法,希望对大家有所帮助,如果大家有任何疑问请给我留言

转载于:https://www.cnblogs.com/wangshucheng/p/11203097.html

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Labelme是一种常用的图像标注工具,它可以帮助用户对图像进行标注和分割。而COCO是一种流行的图像数据集格式,许多深度学习模型都使用COCO格式作为输入数据。因此,将Labelme标注的图像转化为COCO格式是很有用的。 在实现这一目标时,有许多开源的工具和代码可供选择。其,常用的是cocoapi和labelme2coco。cocoapi是由COCO团队开发的,它提供了Python API来处理COCO格式的数据。而labelme2coco是一个第三方库,它可以将Labelme标注的图像转换为COCO格式。 要使用labelme2coco库,需要首先安装它。可以使用pip来安装它,只需在终端输入以下命令: ``` pip install labelme2coco ``` 安装成功后,就可以将Labelme标注的图像转换为COCO格式。首先需要导出Labelme的标注文件,即JSON格式的文件。然后使用以下Python代码: ``` from labelme2coco import labelme2coco labelme_json = "/path/to/labelme.json" # Labelme标注文件的路径 output_json = "/path/to/output.json" # 转换后输出文件的路径 labelme2coco(labelme_json, output_json) ``` 这样就可以将Labelme标注的图像转换为COCO格式,方便后续使用COCO格式的数据进行训练和测试。 ### 回答2: Labelme是一个常用的Python工具,可用于生成用于对象检测任务的标注数据集。它使用JSON格式来记录标注信息。而COCO是一种广泛应用于计算机视觉领域的数据格式,也经常被用于图像分割和对象检测数据集的记录。 如果您需要将Labelme标注数据集转换为COCOJSON格式,可以使用以下两种方式: 1. 使用Python脚本:我们可以编写一个Python脚本,使用Labelme提供的工具函数将JSON文件转换为COCOJSON格式。 您需要提前下载以下软件包:Labelme、numpy和COCOAPI。这里提供一个参考代码,供您参考: ```python import os import json import numpy as np from pycocotools import mask as maskUtils from skimage import measure def labelme2coco(labelme_json): coco_output = {} coco_output['info'] = { 'year': 2021, 'version': '1.0', 'description': 'labelme to coco json format', 'contributor': 'anonymous', 'url': 'http://cocodataset.org', 'date_created': '2021-10-15 00:00:00.000000' } coco_output['licenses'] = [ { 'id': 1, 'name': 'Unknown License', 'url': 'http://creativecommons.org/licenses/by-nc-sa/2.0/' } ] coco_output['categories'] = [ { 'id': 0, 'name': 'background', 'supercategory': 'background', }, { 'id': 1, 'name': 'object', 'supercategory': 'object', } ] with open(labelme_json, 'r') as f: labelme = json.load(f) coco_output['images'] = [] coco_output['annotations'] = [] for i in range(len(labelme['images'])): filename = labelme['images'][i]['file_name'] height = labelme['images'][i]['height'] width = labelme['images'][i]['width'] image_id = i coco_output['images'].append({ 'file_name': filename, 'height': height, 'width': width, 'id': image_id }) for j in range(len(labelme['annotations'])): if labelme['annotations'][j]['image_id'] == i: segmentations = labelme['annotations'][j]['segmentation'] bbox = labelme['annotations'][j]['bbox'] class_id = 1 area = maskUtils.area(maskUtils.frPyObjects(segmentations, height, width)) annotation_id = len(coco_output['annotations']) coco_output['annotations'].append({ 'segmentation': segmentations, 'area': area.tolist()[0], 'iscrowd': 0, 'image_id': image_id, 'bbox': bbox, 'category_id': class_id, 'id': annotation_id }) return coco_output if __name__ == '__main__': labelme_json_file = 'labelme.json' output_file = os.path.splitext(labelme_json_file)[0] + '.coco.json' coco = labelme2coco(labelme_json_file) with open(output_file, 'w') as f: json.dump(coco, f) ``` 上述代码用于将`labelme.json`文件转换为`labelme.coco.json`文件。生成的COCOJSON文件将保存注释标记数据,以及图像和类别信息。 2. 使用网上提供的转换工具:目前也有一些通过网站提供转换服务,帮助用户将Labelme转换为COCOJSON格式的工具。常见的有convertcsv网站和人工智能vip网站,使用方法也很简单,只需上传labelme的JSON文件,并选择需要的输出格式即可。 总之,以上两种方法都适用于将Labelme标注数据集转换为COCOJSON格式。具体选择哪一种方法,根据自己的需求和熟悉程度自行决定即可。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值