如何在Angular4中转换包含对象字符串的单个数组?

时间:2017-10-12 14:55:43

标签: angular typescript

productData(id){
    this.id=id;
    console.log("id"+this.id);
      let headers = new Headers();
    this.createAuthorizationHeader(headers);
return this.http.get(SharedServiceService.productDetails+this.id,{
        headers:headers
        })
      .subscribe(data => { 
          const products = data.json();
          this.productInfo=products;
          this.cart = this.storage.retrieve('cartList');
for(var i=0;i<this.cart.length;i++){
if(this.id==this.cart[i].id){
         this.addedOne=false;
        this.hideCart=true;
        this.continueShopping=false;   
}
}
          
var attr=[];
var storeLoc=[];
          attr.push(this.productInfo);
    console.log("attr",attr);
for(var i=0;i<attr[0].attributes.length;i++){
if((attr[0].attributes[i].name=='Next day store pickup') || (attr[0].attributes[i].name=='Store pickup')|| (attr[0].attributes[i].name=='Delivery price')|| (attr[0].attributes[i].name=='Shipping price')){
    this.prices.push(attr[0].attributes[i]);
        }
    console.log(this.prices);
}

for(var i=0;i<attr[0].attributes.length;i++){
if(attr[0].attributes[i].name=='Stores'){
for(var j=0;j<attr[0].attributes[i].options.length;j++){
    storeLoc.push(JSON.stringify(attr[0].attributes[i].options[j]));
}
}
}
           storeLoc.map((obj) => {
			let data = obj.replace(/”/g, '"');
			data = data.replace(/“/g, '"');
			this.stores.push(JSON.parse(data));
		})
              
          console.log("stores",(this.stores));
          console.log("id info"+JSON.stringify(this.productInfo));
        },
err => {
    console.log("Error!: ",err);
}
        );                              
}
storeLoc= [
                    "{“street”:”3104 Doctors Drive”,”city”:”Los Angeles”,”state code”:”CA”,”state”:”California”,”zip code”:”90017”}",
                    "{“street”:”3358 Cinnamon Lane”,”city”:”San Antonio”,”state code”:”TX”,”state”:”Texas”,”zip code”:”78212”}",
                    "{“street”:”573 May Street”,”city”:”Harold”,”state code”:”KY”,”state”:”Kentucky”,”zip code”:”41635”}",
                    "{“street”:”2836 Cantebury Drive”,”city”:”New York”,”state code”:”NY”,”state”:”New York”,”zip code”:”10013”}",
                    "{“street”:”2935 Kidd Avenue”,”city”:”St George”,”state code”:”AK”,”state”:”Alaska”,”zip code”:”99591”}",
                    "{“street”:”3676 Spirit Drive”,”city”:”Jacksonville”,”state code”:”FL”,”state”:”Florida”,”zip code”:”32220”}",
                    "{“street”:”2134 Shobe Lane”,”city”:”Moody”,”state code”:”AL”,”state”:”Alabama”,”zip code”:”35004”}",
                    "{“street”:”3614 Rhapsody Street”,”city”:”Aiea”,”state code”:”HI”,”state”:”Hawaii”,”zip code”:”96701”}",
                    "{“street”:”1756 Forest Drive”,”city”:”Ashburn”,”state code”:”VA”,”state”:”Virginia”,”zip code”:”22011”}",
                    "{“street”:”686 Upland Avenue”,”city”:”Toledo”,”state code”:”OH”,”state”:”Ohio”,”zip code”:”43609”}",
                    "{“street”:”574 Copperhead Road”,”city”:”Abercrombie”,”state code”:”ND”,”state”:”North Dakota”,”zip code”:”58001”}",
                    "{“street”:”3106 Tecumsah Lane”,”city”:”Van Horne”,”state code”:”IA”,”state”:”Iowa”,”zip code”:”52346”}"
                ]

我正在使用Angular4应用程序。我正在以包含对象字符串的数组格式获得响应。这里我需要使用ngFor.But分别显示每个对象。我无法显示数据,因为对象数据是字符串格式以显示对象属性。她是我的代码:

"options": [
                    "{“street”:”3104 Doctors Drive”,”city”:”Los Angeles”,”state code”:”CA”,”state”:”California”,”zip code”:”90017”}",
                    "{“street”:”3358 Cinnamon Lane”,”city”:”San Antonio”,”state code”:”TX”,”state”:”Texas”,”zip code”:”78212”}",
                    "{“street”:”573 May Street”,”city”:”Harold”,”state code”:”KY”,”state”:”Kentucky”,”zip code”:”41635”}",
                    "{“street”:”2836 Cantebury Drive”,”city”:”New York”,”state code”:”NY”,”state”:”New York”,”zip code”:”10013”}",
                    "{“street”:”2935 Kidd Avenue”,”city”:”St George”,”state code”:”AK”,”state”:”Alaska”,”zip code”:”99591”}",
                    "{“street”:”3676 Spirit Drive”,”city”:”Jacksonville”,”state code”:”FL”,”state”:”Florida”,”zip code”:”32220”}",
                    "{“street”:”2134 Shobe Lane”,”city”:”Moody”,”state code”:”AL”,”state”:”Alabama”,”zip code”:”35004”}",
                    "{“street”:”3614 Rhapsody Street”,”city”:”Aiea”,”state code”:”HI”,”state”:”Hawaii”,”zip code”:”96701”}",
                    "{“street”:”1756 Forest Drive”,”city”:”Ashburn”,”state code”:”VA”,”state”:”Virginia”,”zip code”:”22011”}",
                    "{“street”:”686 Upland Avenue”,”city”:”Toledo”,”state code”:”OH”,”state”:”Ohio”,”zip code”:”43609”}",
                    "{“street”:”574 Copperhead Road”,”city”:”Abercrombie”,”state code”:”ND”,”state”:”North Dakota”,”zip code”:”58001”}",
                    "{“street”:”3106 Tecumsah Lane”,”city”:”Van Horne”,”state code”:”IA”,”state”:”Iowa”,”zip code”:”52346”}"
                ]

这是我需要显示的数组。如何在Angular4中实现此数组的代码?

3 个答案:

答案 0 :(得分:0)

您只需要解析(JSON.parse)您的数据,使其格式为* ngFor可以使用。如果您遇到问题,那么解析可能会出现语法问题,因此您应该引起注意。一旦你解决了解析它就会起作用,因为这正是为了处理而创建的那种迭代* ngFor。

答案 1 :(得分:0)

&#13;
&#13;
var options = [
		"{“street”:”3104 Doctors Drive”,”city”:”Los Angeles”,”state code”:”CA”,”state”:”California”,”zip code”:”90017”}",
		"{“street”:”3358 Cinnamon Lane”,”city”:”San Antonio”,”state code”:”TX”,”state”:”Texas”,”zip code”:”78212”}",
		"{“street”:”573 May Street”,”city”:”Harold”,”state code”:”KY”,”state”:”Kentucky”,”zip code”:”41635”}",
		"{“street”:”2836 Cantebury Drive”,”city”:”New York”,”state code”:”NY”,”state”:”New York”,”zip code”:”10013”}",
		"{“street”:”2935 Kidd Avenue”,”city”:”St George”,”state code”:”AK”,”state”:”Alaska”,”zip code”:”99591”}",
		"{“street”:”3676 Spirit Drive”,”city”:”Jacksonville”,”state code”:”FL”,”state”:”Florida”,”zip code”:”32220”}",
		"{“street”:”2134 Shobe Lane”,”city”:”Moody”,”state code”:”AL”,”state”:”Alabama”,”zip code”:”35004”}",
		"{“street”:”3614 Rhapsody Street”,”city”:”Aiea”,”state code”:”HI”,”state”:”Hawaii”,”zip code”:”96701”}",
		"{“street”:”1756 Forest Drive”,”city”:”Ashburn”,”state code”:”VA”,”state”:”Virginia”,”zip code”:”22011”}",
		"{“street”:”686 Upland Avenue”,”city”:”Toledo”,”state code”:”OH”,”state”:”Ohio”,”zip code”:”43609”}",
		"{“street”:”574 Copperhead Road”,”city”:”Abercrombie”,”state code”:”ND”,”state”:”North Dakota”,”zip code”:”58001”}",
		"{“street”:”3106 Tecumsah Lane”,”city”:”Van Horne”,”state code”:”IA”,”state”:”Iowa”,”zip code”:”52346”}"
	];
  
  var newArray = [];
  
  options.map((obj) => {
			let data = obj.replace(/”/g, '"');
			data = data.replace(/“/g, '"');
			newArray.push(JSON.parse(data));
		})
    
   console.log('newArray', newArray);
&#13;
&#13;
&#13;

答案 2 :(得分:0)

您尝试解析的字符串存在问题。将您的JSON对象粘贴到下面的网站(json解析器和格式化程序)。

https://jsonformatter.curiousconcept.com/

您可以调试并查找数据问题。

OR

不是理想的解决方案:使用字符串操作来构建对象。在&#34; 上使用string.split,您将获得字符串数组,使用该数组可以将其转换为键值对对象。