SORU
5 Mayıs 2011, PERŞEMBE


Bir JSON yanıt NodeJS (nesne/dizi dize JSON dönüştürme)nesne

Geri kod sonu için bir yeniyim ve bana bir json dize cevap verecek bir işlev oluşturmak için çalışıyorum. Ben şu anda bir örnek bu

function random(response) {
  console.log("Request handler 'random was called.");
  response.writeHead(200, {"Content-Type": "text/html"});

  response.write("random numbers that should come in the form of json");
  response.end();
}

Bu temelde sadece dize yazdırır "json şeklinde gelmesi gerektiğini rasgele sayılar". Bunu yapmak istiyorum ne sayılar ne bir json dize ile yanıt. Farklı bir içerik türü koymak gerekiyor mu? bu işlev, başka bir istemci tarafında bu değeri geçmesi gerekir?

Yardımlarınız için teşekkürler!

CEVAP
21 Mart 2012, ÇARŞAMBA


function random(response) {
  console.log("Request handler random was called.");
  response.writeHead(200, {"Content-Type": "application/json"});
  var otherArray = ["item1", "item2"];
  var otherObject = { item1: "item1val", item2: "item2val" };
  var json = JSON.stringify({ 
    anObject: otherObject, 
    anArray: otherArray, 
    another: "item"
  });
  response.end(json);
}

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • jocc talking shit

    jocc talking

    6 NİSAN 2007
  • mliskIT

    mliskIT

    29 Mart 2012
  • UKF

    UKF

    2 Aralık 2009