This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm whether you accept or reject these cookies being set.

A cookie will be stored in your browser regardless of choice to prevent you being asked this question again. You will be able to change your cookie settings at any time using the link in the footer.

Themabewertung:
  • 0 Bewertung(en) - 0 im Durchschnitt
  • 1
  • 2
  • 3
  • 4
  • 5
API Abfrage
#1
Question 
Hallo zusammen, bin hier irwie am verzweifeln, vorweg ich bin noch nicht so lange dabei mich mit js auseinander zu setzen.

Ich frage eine Api ab und möchte dies in einer json Datei speichern. klappt auch soweit ganz gut, doch ist das was gespeichert wird zu viel für mich, also nicht relevantes zeug.

mein Code:
function getPriceList() {
	request(priceUrl, (error, response, body) => {
		if (error || response.statusCode !== 200) return console.log(`Error: ${error} - Status Code: ${response.statusCode}`);
		//console.log("DEBUG: " + body);
		fs.writeFileSync("prices.json", JSON.stringify(JSON.parse(body), null, 2));
        
	});
}

getPriceList();

prices.json Snippet:
{
  "status": "success",
  "prices": [
    {
      "app_id": "730",
      "context_id": "2",
      "market_hash_name": "'Blueberries' Buckshot | NSWC SEAL",
      "price": "1.71",
      "pricing_mode": "market",
      "skewness": "-0.49",
      "created_at": 1631410301,
      "icon_url": null,
      "name_color": null,
      "quality_color": null,
      "rarity_color": null,
      "instant_sale_price": null
    },
    {
      "app_id": "730",
      "context_id": "2",
      "market_hash_name": "'The Doctor' Romanov | Sabre",
      "price": "3.06",
      "pricing_mode": "market",
      "skewness": "-0.05",
      "created_at": 1631419491,
      "icon_url": null,
      "name_color": null,
      "quality_color": null,
      "rarity_color": null,
      "instant_sale_price": "1.22"
    },
    {
      "app_id": "730",
      "context_id": "2",
      "market_hash_name": "'Two Times' McCoy | TACP Cavalry",
      "price": "1.23",
      "pricing_mode": "market",
      "skewness": "-0.11",
      "created_at": 1631407484,
      "icon_url": null,
      "name_color": null,
      "quality_color": null,
      "rarity_color": null,
      "instant_sale_price": null
    }
  ]
}

meine Frage nun, ist es irgendiwe möglich, das nur noch dies:
{
  "'Blueberries' Buckshot | NSWC SEAL": 1.71,
  "'The Doctor' Romanov | Sabre": 3.06,
  "'Two Times' McCoy | TACP Cavalry": 1.23
}
in der prices.json Datei ankommt?

Also quasi der

"market_hash_name": price



mfG

André
Zitieren
#2
Hast du mal ein Link zu der API , oder kannst du mal den Inhalt der API kopieren und den ganzen Code zeigen, den du nutzt, um die Daten zu holen?
Wenn die Daten so aussehen würden
var body={
  "status": "success",
  "prices": [
    {
      "app_id": "730",
      "context_id": "2",
      "market_hash_name": "'Blueberries' Buckshot | NSWC SEAL",
      "price": "1.71",
      "pricing_mode": "market",
      "skewness": "-0.49",
      "created_at": 1631410301,
      "icon_url": null,
      "name_color": null,
      "quality_color": null,
      "rarity_color": null,
      "instant_sale_price": null
    },
    {
      "app_id": "730",
      "context_id": "2",
      "market_hash_name": "'The Doctor' Romanov | Sabre",
      "price": "3.06",
      "pricing_mode": "market",
      "skewness": "-0.05",
      "created_at": 1631419491,
      "icon_url": null,
      "name_color": null,
      "quality_color": null,
      "rarity_color": null,
      "instant_sale_price": "1.22"
    },
    {
      "app_id": "730",
      "context_id": "2",
      "market_hash_name": "'Two Times' McCoy | TACP Cavalry",
      "price": "1.23",
      "pricing_mode": "market",
      "skewness": "-0.11",
      "created_at": 1631407484,
      "icon_url": null,
      "name_color": null,
      "quality_color": null,
      "rarity_color": null,
      "instant_sale_price": null
    }
  ]
};
Dann würde ich die Daten die brauchst da rausholen und in ein neus Array schreiben.
var helper=[];
for(x=0;x<body.prices.length;x++){
   helper.push(body.prices[x].market_hash_name);
}
console.log(helper);
Das sieht dann so aus
['Blueberries' Buckshot | NSWC SEAL", "'The Doctor' Romanov | Sabre", "'Two Times' McCoy | TACP Cavalry"]
Je nachdem was da vor oder danach passiert, kann man sich das ja anpassen.
Falls dir das so nicht reicht , zeig ,uns mal den Inhalt der API und den Code den du nutzt, um den Inhalt zu holen.
Ggf. , vileiecht auch noch kurz erklären was dann passieren soll , ob der Inhalt im Array reicht , oder noch was anderes passiert
Als Lösung markieren Zitieren
#3
Danke für die Antwort, ich schreibe später mal alles nochmal ausführlich wenn ich am PC bin.

PS: wollte mich gerade registrieren hier, aber beim absenden läd er die members.php datei runter
Als Lösung markieren Zitieren
#4
ok checke das gleich mal mit den registrieren
Als Lösung markieren Zitieren
#5
Habe mal gerade ein Account eröffnet , und das funktioniert ohne Probleme.
Wann kommt den bei dir das Problem genau?
Als Lösung markieren Zitieren


Gehe zu:


Benutzer, die gerade dieses Thema anschauen: 1 Gast/Gäste