search
HomeWeb Front-endJS TutorialA JavaScript function parses URL parameters into Json objects_javascript skills

Question: Please write a JavaScript function parseQueryString, which is used to parse URL parameters into an object.
eg: var obj=parseQueryString(url);

Three forms of creating objects:
1:

var Person=new Object();
Person.name="Sun";
Person.age=24;

Two:

var Person=new Object();
Person["name"]="Sun";
Person["age"]=24;

Three:
Object literal expression

var Person={
name: "Sun",
age: 24
}

PS:
1. In this example, it is more suitable to use the second form, adding elements
to obj 2. split("&"), if the url has only one parameter and there is no "&", no error will be reported, and only array[0]

will be returned.
function parseQueryString(url)
{
var obj={};
var keyvalue=[];
var key="",value=""; 
var paraString=url.substring(url.indexOf("?")+1,url.length).split("&");
for(var i in paraString)
{
keyvalue=paraString[i].split("=");
key=keyvalue[0];
value=keyvalue[1];
obj[key]=value; 
} 
return obj;
}

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
在Java中使用GSON如何获取JSON对象的所有键?在Java中使用GSON如何获取JSON对象的所有键?Aug 30, 2023 pm 11:45 PM

AGson isalibrarythatcanbeusedtoparseJavaobjectstoJSON andvice-versa.ItcanalsobeusedtoconvertaJSONstringtoanequivalentJavaobject.InordertoparsejavaobjecttoJSONorJSONtojavaobject,weneedtoimportcom.google.gson packageintheJava

如何在Go语言中使用正则表达式匹配URL参数如何在Go语言中使用正则表达式匹配URL参数Jul 12, 2023 pm 03:46 PM

如何在Go语言中使用正则表达式匹配URL参数正则表达式是一种强大的工具,可以用来匹配和处理字符串。在网络开发中,经常需要从URL中提取特定的参数。Go语言提供了标准库中的正则表达式包(regexp),可以方便地使用正则表达式来匹配URL参数。本文将介绍如何在Go语言中使用正则表达式来匹配URL参数,并提供一些实用的代码示例。导入regexp包首先,我们需要导

PHP正则表达式实战:匹配URL参数PHP正则表达式实战:匹配URL参数Jun 23, 2023 am 10:55 AM

随着互联网技术的发展和应用的不断拓展,URL参数已成为我们日常开发中常见需要处理的数据类型。在实际的开发环境中,我们经常需要根据URL参数匹配出一些特定的信息,例如提取某个参数的值、判断参数是否符合格式等等。正则表达式可以帮助我们对URL参数进行快速地匹配和处理。本篇文章将介绍PHP正则表达式实战中针对URL参数匹配所使用的相关知识和技巧,并给出实例代码加以

我们如何在Java中合并两个JSON对象?我们如何在Java中合并两个JSON对象?Aug 26, 2023 am 08:01 AM

JSON是一种轻量级数据交换格式,JSON的格式是键值对。JSONObject可以解析字符串中的文本以生成类似地图的对象并支持java.util.Map接口。我们可以使用org.json.simple.JSONObject在Java中合并两个JSON对象。我们可以使用putAll()合并两个JSON对象下面的程序中的方法(继承自接口 java.util.Map)。示例importjava.util.Date;importorg.json.simple.JSONObject;publicclass

如何在Java中使用JsonConfig将bean转换为JSON对象并排除某些属性?如何在Java中使用JsonConfig将bean转换为JSON对象并排除某些属性?Sep 01, 2023 pm 06:37 PM

JsonConfig 类是一个帮助配置序列化过程的实用类。我们可以使用JsonConfig 类的setExcludes()方法将一个bean转换为一个JSON对象,并排除其中的一些属性,并将这个JSON配置实例传递给JSONObject的静态方法fromObject()的参数。语法publicvoidsetExcludes(String[]excludes)Inthebelowexample,wecanconvertbeantoaJSONobjectbyexc

PHP中如何处理和操作URL参数的数据类型PHP中如何处理和操作URL参数的数据类型Jul 18, 2023 pm 01:33 PM

PHP中如何处理和操作URL参数的数据类型在web开发中,URL参数是非常常见的一种数据传递方式。通过URL参数,我们可以在不同页面之间传递数据,实现数据的交互和传递。在PHP中,处理和操作URL参数的数据类型是一项重要的技能。本文将介绍如何在PHP中处理和操作URL参数的不同数据类型,并附带代码示例。获取URL参数获取URL参数是处理和操作URL参数的第一

如何使用Java中的排除过滤器将一个bean转换为JSON对象?如何使用Java中的排除过滤器将一个bean转换为JSON对象?Aug 18, 2023 pm 07:05 PM

可以使用JsonConfig类来配置序列化过程。我们可以使用JsonConfig的setJsonPropertyFilter()方法来设置在序列化为JSON时的属性过滤器。我们需要通过重写PropertyFilter接口的apply()方法来实现一个自定义的PropertyFilter类。如果属性将被过滤掉,则返回true,否则返回false。语法publicvoidsetJsonPropertyFilter(PropertyFilterjsonPropertyFilter)Exampleimp

使用PHP过滤和验证URL参数的最佳实践使用PHP过滤和验证URL参数的最佳实践Jul 07, 2023 am 08:49 AM

使用PHP过滤和验证URL参数的最佳实践在开发Web应用过程中,经常会使用到URL参数。如何正确地过滤和验证这些参数,是保证应用程序安全的重要一步。本文将介绍一些PHP中过滤和验证URL参数的最佳实践,并提供相应的代码示例。过滤URL参数URL参数可能包含不可信的用户输入,因此需要进行过滤,以防止恶意代码或非法输入的传递。下面是使用filter_input函

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft