2014-12-03 162 views
-1

我需要什么如何使用foreach循环

  • 我需要获取整个JSON数据和DIV追加ACCES JSON数据。

jQuery代码

  $.ajax({ 
      type: "GET", 
      contentType: 'application/json; charset=utf-8', 
      url: "http://local.10times.com/app.php/ajax?for=speakers&id="+id, 
      dataType: 'json', 
      async: true, 
      cache: false, 
      success: function(html) 
      { 


       var i=0; 
       $.each(html, function(i, obj) { 
       //use obj.id and obj.name here, for example: 
       console.log(obj[0].metadata.summary); 
       var obj1=obj[i].metadata.summary; 
       i++; 
       alert(obj1);// undefined 


       $(".speakers").append(obj[0].metadata.summary); 
       i++; 
       }); 

      } 
     }); 

输出

 0: Objectmetadata: Objectsummary: "Raj is the founder of GSF Accelerator, India’s first multi-city Tech Accelerator, backed by 20 iconic Digital founders and 5 leading venture funds frversaw (as President, Reliance E". 

JSON字符串

  data: [ 
       { 
       metadata: { 
       summary: "Rajesh is the founder of GSF Accelerator, India’s first multi-city Tech Accelerator, backed by 20 iconic Digital founders and 5 leading venture funds from across the world. Rajesh is also the founder of GSF Superangels network and curator of the Global Superangels Forum. He is the co-host and curator of Founders Forum India. Rajesh has made over 15 investments as an active and engaged angel investor.His journey has been a continuum of re-invention waves. In the first wave, he brought private radio (Times FM) to India in early nineties, and redefined Indian youth culture. In his second wave, he was at the cusp of early Internet culture in India and built Indiatimes.com, the most valuable Internet business in India in 2005 (funded by Sequoia). In the third wave, Rajesh was at the centre of a massive transformation of the Indian film industry, where he oversaw (as President, Reliance Entertainment) the corporatization of mom and pop Bollywood businesses into multi-billion dollar enterprise. At Reliance, Rajesh found himself at the centre of cross-border investment and alignment between Hollywood and Indian Film Industry and one of the biggest investment deals (DreamWorks) in Hollywood. Currently in his fourth wave, Rajesh is building the vital building blocks of a nascent early stage startup ecosystem in India, and sees this as the biggest and most exciting challenge that he has faced in his life.Rajesh is an alumnus of Harvard Business School (AMP), and has a fellowship from London School of Economics. Twitter URL: http://twitter.com/rajeshsawhneyWebsite: www.gsfindia.com" 
       } 
       }, 
       { 
       metadata: { 
       summary: "Building and scaling businesses focused on simple yet powerful consumer products has been the dream, and this has come to life both at Skype, Gumtree and now most recently at LimeRoad.com. We are the the start of the journey to build the most extensive and engaging lifestyle platform ever. Our sole objective is to make the discovery experience on LimeRoad so delightful, that we become the online destination to find gorgeous yet affordable products.Earlier, with 13m users at Gumtree, we built UK's largest horizontal classifieds business. In c. two years, we went from a No. 3 position to market leading positions in jobs and consumer-to-consumer car sales, growing visits by 35% y-o-y in the UK (marketing spend remaining unchanged). Gumtree required transformational leadership, and I spent the better part of my time there scaling team capabilities, product, sales, marketing and CS functions, whilst re-architecting the financials towards core revenue streams.Prior to this, Skype was a powerful leadership development experience - being part of the executive management team of a high growth technology company, transitioning from founder–led-startup with 3 CEO changes in 2 years, whilst also building the most dramatic upgrade of the product. What a roller coaster, but not one to trade out of!Specialties: Scaling businesses, by setting direction and managing execution around product development, marketing, sales and operations. Building and empowering high performing teams." 
       } 
       }, 
       { 
       metadata: { 
       summary: "I have led various design projects providing better browsing experience for users and helping businesses increase their conversion rates. I have experience in strategising and conceptualising designs for various domains like Ecommerce, Social Networking sites, Web based intranets, Banking and finance applications etc. I have helped companies look at their web logs and usage data to interpret user behaviour. I has keen interest in designing for the new age internet industry. I have been closely following the change in users and their online behaviours.I also specialise in conducting User researches and Usability tests across various domains and cultures.I have led several contextual innovation projects for companies like Intel, HP, GE etc conducting researches in emerging markets of India and China and innovating products catering to these markets. I have also performed Usability tests for websites, desktop applications, mobile phone devices for companies like Nokia, AOL, Vodaphone, Australian government websites and several Indian websites. I have experience in conducting Remote Usability Testing as well.I am a co founder of Techved Design which is a User Experience consultancy in Mumbai. The consultancy helps clients understand user behaviour and provide solutions based on User Centred Design Methodology. It also conducts workshops to train people on principles of Usability. Techved Design has also partnered with consultancies in China and Australia to provide a suite of services across the Asia Pacific region." 
       } 
       }, 
       { 
       metadata: { 
       summary: "Goal oriented engineering executive with keen eye on execution, organizational efficiency and long term strategy, developing enterprise class eCommerce Marketplace Platform at the Fastest Growing eCommerce Marketplace in India - ShopClues.com.What makes this possible:• Extensive experience in developing highly scalable Consumer Internet technologies (like ecommerce platforms, Content Delivery, etc.)• Create and Manage rockstar team focused in skills, organizational efficiency, process optimization for delivering world class products at lightning speed.• Collaborate with all lines of business, solution providers, technologists and thought leaders.• Finally, build a culture and work environment that is goal driven, empower and reward individuals for accomplishing the impossibles.• I love what I do.Specialties: • Application & Enterprise Solutions • Product Strategy and direction• Product Architecture• Engineering Management (Fortune 100 & Startups)• Project Management• Process Improvement• On-Time Product Delivery • Product Design, Development , Quality & UsabilityIndustry experience:• Consumer Marketing - Social, Local & Mobile.• eCommerce platforms & eCommerce Marketplace platforms• Customer Relationship Management• Data Loss and Prevention - Data security• Software-as-a-Service SaaS" 
       } 
       }, 

输出 enter image description here

具有

  • 的console.log

    问题IM(OBJ [0] .metadata.summary);我得到了json字符串的第一个索引。

    but while iterating loop Uncaught TypeError: Cannot read property 'metadata' of undefined. 
    
  • 哪里错了,任何建议都是最受欢迎的。
+0

后的JSON请 – deitch 2014-12-03 07:34:11

+0

您的JSON结果是有效的? – Riad 2014-12-03 07:56:54

+0

我已经更新了我有效的JSON – user2818060 2014-12-03 08:07:15

回答

-1

尝试 的console.log(OBJ [0] .Objectmetadata.Objectsummary) 它将工作

+0

访问总之我已经尝试了工作,但如何获取整个总结 – user2818060 2014-12-03 07:36:40

+0

遗漏的类型错误:无法读取未定义 – user2818060 2014-12-03 07:42:44

-1

无需额外的变量i,$。每个已经给你一个指标。

$.each(html.data, function (index, obj) { 
    //use obj.id and obj.name here, for example: 
    console.log(obj.metadata.summary, $(".speakers")); 
    $(".speakers").append('<div class="speaker">' + obj.metadata.summary + '</div>'); 

}); 
+0

$。每个内返回一个对象的特性“Objectsummary”数组,而不是完整的数组,因此不需要[0],只需使用obj。 – Indra 2014-12-03 07:46:44

+0

Uncaught TypeError:无法读取未定义的属性'汇总' – user2818060 2014-12-03 07:47:24

+0

我无法理解这里出现了什么问题,为什么代码中有-1。 – Indra 2014-12-03 07:47:25

-1

问题你是因为你是直接访问您的JSON循环得到。

首先解码的JSON数据到阵列如下面

$数据= json_decode($数据,TRUE);

然后将其添加到foreach循环

 foreach ($data as $key => $value) 

     { 

     } 

现在访问循环您的数据。希望这对你能正常工作。

+0

这不是'PHP' ......他正在JS – Riad 2014-12-03 07:55:56

+0

我知道,但我想给概念,他 – 2014-12-03 08:00:33

+0

回答与具体的解决办法的问题......你的答案是不相关的,因为这是你正在使用不同的语言这不是一个概念性问题。 – Riad 2014-12-03 08:02:17

0

试试这个

//Ajax Done catch JSON from PHP 
      request.done(function(dataset){ 
       for (var index in dataset){ 
        dataPHPtoJsJS=dataset[index].metadata.summary; 
       } 
      } 
+0

我无法理解你的逻辑 – user2818060 2014-12-03 08:06:55

+0

(数据集中的var索引)是相同的。dataPHPtoJsJS是一个变量,你可以在其中为你的数据对象的每个索引汇总文本 – zwitterion 2014-12-03 08:16:56