gpt4 book ai didi

angular - 将复杂对象转换为查询字符串

转载 作者:太空狗 更新时间:2023-10-29 18:25:01 28 4
gpt4 key购买 nike

如何将复杂对象转换为用于 WebApi 的查询字符串:

export enum FilterCondition {
Equal,
NotEqual,
GreaterThan,
LessThan,
GreaterThanEqual,
LessThanEqual
}

export class QueryParameter {
propertyName: string;
filterCondition: FilterCondition
value: string;
}

export class QueryOptions {
queryParameters: QueryParameter[] = new Array<QueryParameter>();
}

我正在尝试编写一个通用函数来将复杂类型转换为基本查询我写了这个函数来将一个简单的对象转换为查询字符串

export class QueryStringBuilder {
static BuildParametersFromSearch<T>(obj: T): URLSearchParams {
let params: URLSearchParams = new URLSearchParams();

const objectKeys = Object.keys(obj) as Array<keyof T>;
for (let key of objectKeys) {
params.set(key, obj[key])
}

return params;
}
}

但是我无法理解如何对具有枚举和数组等的复杂对象递归执行此操作?任何人都可以指出正确的方向或已经这样做的东西吗?

最佳答案

如果有人想知道如何做到这一点,我已经编写了一个应该与 c# .Net Core 1.1 和 Typescript 2.2.2 WebApi 一起使用的扩展,看起来像这样。

请记住在您使用它的地方也包含这两个导入

import { URLSearchParams } from '@angular/http';
import 'rxjs/add/operator/map'

export class QueryStringBuilder {
static BuildParametersFromSearch<T>(obj: T): URLSearchParams {
let params: URLSearchParams = new URLSearchParams();

if (obj == null)
{
return params;
}

QueryStringBuilder.PopulateSearchParams(params, '', obj);

return params;
}

private static PopulateArray<T>(params: URLSearchParams, prefix: string, val: Array<T>) {
for (let index in val) {
let key = prefix + '[' + index + ']';
let value: any = val[index];
QueryStringBuilder.PopulateSearchParams(params, key, value);
}
}

private static PopulateObject<T>(params: URLSearchParams, prefix: string, val: T) {
const objectKeys = Object.keys(val) as Array<keyof T>;

if (prefix) {
prefix = prefix + '.';
}

for (let objKey of objectKeys) {

let value = val[objKey];
let key = prefix + objKey;

QueryStringBuilder.PopulateSearchParams(params, key, value);
}
}

private static PopulateSearchParams<T>(params: URLSearchParams, key: string, value: any) {
if (value instanceof Array) {
QueryStringBuilder.PopulateArray(params, key, value);
}
else if (value instanceof Date) {
params.set(key, value.toISOString());
}
else if (value instanceof Object) {
QueryStringBuilder.PopulateObject(params, key, value);
}
else {
params.set(key, value.toString());
}
}
}

这适用于我目前使用的所有复杂类型。

编辑 用法我已经包含了所有导入语句,我认为重要的是 RequestOptionsArgs、RequestOptions,但我记得它们是必需的,所以以防万一我已经包含了它们。

import { Injectable } from '@angular/core';
import { Http, Headers, RequestOptionsArgs, RequestOptions } from '@angular/http';
import { Observable } from 'rxjs/Rx';
import { IHasId } from '../interfaces/interfaces';
import 'rxjs/add/operator/map';

import { QueryOptions, IFilterNode } from "../models/queryOptions";
import { QueryStringBuilder } from "../models/QueryStringBuilder";

import 'rxjs/add/operator/map'

@Injectable()
export class ProviderBase<T extends IHasId> {

getList(filterParams?: IFilterNode): Observable<T[]> {
var searchParams = QueryStringBuilder.BuildParametersFromSearch(filterParams);

let requestArguments: RequestOptionsArgs = new RequestOptions({ search: searchParams });
return this.http.get(`${this.apiUrl}/${this.route}`, requestArguments).map(res => <T[]>res.json());
}
}

关于angular - 将复杂对象转换为查询字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45603683/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com