Android JSONobject - How to Loop Through a Flat JSON Object to Get Each Key and Value

Android JSONObject - How can I loop through a flat JSON object to get each key and value

Use the keys() iterator to iterate over all the properties, and call get() for each.

Iterator<String> iter = json.keys();
while (iter.hasNext()) {
String key = iter.next();
try {
Object value = json.get(key);
} catch (JSONException e) {
// Something went wrong!
}
}

Iterating through JSONObject and getting it's deeper childs child values

Iterate through resultData and parse.

private void getVendorsList(JsonObject body) {
JsonArray jsonArray = body.getAsJsonArray("data");
List<VendorModel> vendorModelList = new ArrayList<>();
if(jsonArray != null) {
for (int i = 0; i < jsonArray.size(); i++) {
JsonObject dataObject = jsonArray.get(i).getAsJsonObject();
if(dataObject != null) {
JsonObject resultObject = dataObject.getAsJsonObject("resultData");
if(resultObject != null) {

//Manual parse
int id = resultObject.get("id").getAsInt();
String vendorName = resultObject.get("vendorName").getAsString();
String shortDescription = resultObject.get("shortDescription").getAsString();
String imageUrl = resultObject.get("imageUrl").getAsString();
String url = resultObject.get("url").getAsString();

VendorModel vendorModel = new VendorModel(id,vendorName, shortDescription, imageUrl, url);
vendorModelList.add(vendorModel);

//Parse using Gson. Also try this instead of above if your json key and model property are aligned
/*VendorModel vendorModel = new Gson().fromJson(resultObject, VendorModel.class);
vendorModelList.add(vendorModel);*/
}
}
}
}

Log.d("vendorsList", String.valueOf(vendorModelList));
}

N.B: You are mixing org.json with google.gson in your implementation.

Loop through json, with multiple key values (some same value)

You can save a counter for each post and check that in your loop.

Try this:

$pCounter = 0; // for Post counter
$artistTitle = '';
foreach($gallery as $image) {
if ($image->Artist_Title !== $artistTitle) {
$pCounter = 0;
}
$counter++;
echo 'wget https://www.accessallareas.info/images/' . $image->Gallery_Image_Filename . ' -O ' . $image->Artist_Title . '-gallery-'. $pCounter . '.jpg';
echo '<br>';
$artistTitle = $image->Artist_Title;
}

Note:

  • You had a typo in your code. Article_Title should be Artist_Title.
  • You need to use json_decode($json); at the top of your code to convert JSON into an array of objects.

How to iterate over JSONObject and found id by value which already known, for example I want to get id of value ТС-41 how can I do it?

You'll need to iterate over the JSONObject keys and compare the value until a match.

final String knownValue = "TC-41";
final Iterator<String> iterable = jsonObject.keys();
while (iterable.hasNext()) {
String key = iterable.next();
if (jsonObject.getString(key).equals(knownValue)) {
// key has the value you are looking for
return;
}
}

Is there a quick way to iterate through a JSON object, changing the title of each object to a value of one of it's key values?

Simple for loop can be used to create a new object. Check for duplicate itemHash values is recommended just in case. Also, better to use new object instead of mutating the original one, to avoid some edge case issues.





var result = {}, obj = {"28595":{"displayProperties":{"description":"I wish to be strategic.","name":"Helm of the Great Hunt","icon":"/common/destiny2_content/icons/c9ff2846a1474f315305e4f9bc413b2e.jpg","hasIcon":true},"scope":1,"sourceString":"Source: Last Wish raid.","sourceHash":2455011338,"itemHash":2274520361,"acquisitionInfo":{"runOnlyAcquisitionRewardSite":false},"stateInfo":{"requirements":{"entitlementUnavailableMessage":"Requires Destiny 2: Forsaken"}},"presentationInfo":{"presentationNodeType":2,"parentPresentationNodeHashes":[1847361717,558738844],"displayStyle":3},"hash":28595,"index":686,"redacted":false,"blacklisted":false},"637236":{"displayProperties":{"description":"I wish to be wise.","name":"Hood of the Great Hunt","icon":"/common/destiny2_content/icons/b70e19e236a3cfd0fc762be6f2d226d9.jpg","hasIcon":true},"scope":1,"sourceString":"Source: Last Wish raid.","sourceHash":2455011338,"itemHash":3251351304,"acquisitionInfo":{"runOnlyAcquisitionRewardSite":false},"stateInfo":{"requirements":{"entitlementUnavailableMessage":"Requires Destiny 2: Forsaken"}},"presentationInfo":{"presentationNodeType":2,"parentPresentationNodeHashes":[1375933816,282080253],"displayStyle":3},"hash":637236,"index":1202,"redacted":false,"blacklisted":false}}


for (var key in obj)

if (obj[key].itemHash in result)

console.log('duplicate itemHash value:', obj[key].itemHash)

else

result[obj[key].itemHash] = obj[key]



console.log( result )

JSONArray recognized as JSONObject

Json array format is

"appelliLista":[
{....},
{....}
]

your json element appelliLista is a json object so cannot parse to array



Related Topics



Leave a reply



Submit