“iterować obiekt JSON” Kod odpowiedzi

JavaScript Iterate nad JSON

var person={
 	first_name:"johnny",
  	last_name: "johnson",
	phone:"703-3424-1111"
};
for (var property in person) {
  	console.log(property,":",person[property]);
}
Grepper

pętla za pośrednictwem Json Object JavaScript

var p = {
    0: "value1",
    "b": "value2",
    key: "value3"
};

for (var key of Object.keys(p)) {
    console.log(key + " -> " + p[key])
}
Inquisitive Ibex

Jak iterować nad Jsonobject

import org.json.JSONObject;

public static void printJsonObject(JSONObject jsonObj) {
    for (String keyStr : jsonObj.keySet()) {
        Object keyvalue = jsonObj.get(keyStr);

        //Print key and value
        System.out.println("key: "+ keyStr + " value: " + keyvalue);

        //for nested objects iteration if required
        //if (keyvalue instanceof JSONObject)
        //    printJsonObject((JSONObject)keyvalue);
    }
}
Smoggy Scarab

iterować obiekt JSON

var p = {
    "p1": "value1",
    "p2": "value2",
    "p3": "value3"
};

for (var key in p) {
    if (p.hasOwnProperty(key)) {
        console.log(key + " -> " + p[key]);
    }
}
Motionless Meerkat

Przechodząc przez wszystkie atrybuty obiektu JavaScript

let a = {x: 200, y: 1}
let attributes = Object.keys(a)
console.log(attributes)
//output: ["x", "y"]
Excited Eel

Jak iterować nad Jsonobject

import org.json.JSONObject;

public static void printJsonObject(JSONObject jsonObj) {
    jsonObj.keySet().forEach(keyStr ->
    {
        Object keyvalue = jsonObj.get(keyStr);
        System.out.println("key: "+ keyStr + " value: " + keyvalue);

        //for nested objects iteration if required
        //if (keyvalue instanceof JSONObject)
        //    printJsonObject((JSONObject)keyvalue);
    });
}
Smoggy Scarab

Odpowiedzi podobne do “iterować obiekt JSON”

Pytania podobne do “iterować obiekt JSON”

Więcej pokrewnych odpowiedzi na “iterować obiekt JSON” w JavaScript

Przeglądaj popularne odpowiedzi na kod według języka

Przeglądaj inne języki kodu