2014-12-03 163 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