Viewed   92 times

I have a JSON array

      "id": "8080",
      "content": "foo"
      "id": "8097",
      "content": "bar"

How would I search for 8097 and get the content?



Use the json_decode function to convert the JSON string to an array of object, then iterate through the array until the desired object is found:

$str = '{
      "id": "8080",
      "content": "foo"
      "id": "8097",
      "content": "bar"

$json = json_decode($str);
foreach ($json->people as $item) {
    if ($item->id == "8097") {
        echo $item->content;
Wednesday, September 14, 2022

decode your json as array and iterate it as any array as flowing:

$json_decoded= json_decode($json,true);


foreach ($json_decoded as $key => $val) {
    echo "Article ".$val["key"]."n" ;
    echo $tab."Authors :n";
    foreach ($val["data"]["authors"] as $key => $author){
        echo $tab.$tab. ($key+1) ." - ".$author["firstName"]. " ".$author["lastName"]."n";

    echo $tab."Article Title: ".$val["data"]["articleTitle"] ."n";
    echo $tab."Publication Title: ".$val["data"]["pubTitle"] ."n";
    echo $tab."Key: ".$val["key"]."n";

run on codepad

and you can use the same method for xml as flowing:

$xml = simplexml_load_string($xmlstring);
$json = json_encode($xml);
$json_decoded = json_decode($json,TRUE);
//the rest is same

for xml you can use the SimpleXml's functions or DOMDocument class


to know the structure of your data that api return to you after it converted to array use var_dump($your_decoded_json) in debuging

Sunday, October 30, 2022

So VBA.CallByName also accesses elements in an array as well as find the array's length

'Microsoft Script Control 1.0;  {0E59F1D2-1FBE-11D0-8FF2-00A0D10038BC}; C:WindowsSysWOW64msscript.ocx

Private Sub TestJSONParsingArrayWithCallByName()

    Dim oScriptEngine As ScriptControl
    Set oScriptEngine = New ScriptControl
    oScriptEngine.Language = "JScript"

    Dim sJsonString As String
    sJsonString = "[ 1234, 2345 ]"

    Dim objJSON As Object
    Set objJSON = oScriptEngine.Eval("(" + sJsonString + ")")

    '* Using VBA.CallByName we get the length of the array

    Dim lLength As Long
    lLength = VBA.CallByName(objJSON, "length", VbGet)

    Debug.Assert lLength = 2

    '* Believe or not one uses "0","1",.... with callbyname to get an element
    Debug.Assert VBA.CallByName(objJSON, "0", VbGet) = 1234
    Debug.Assert VBA.CallByName(objJSON, "1", VbGet) = 2345

End Sub
Wednesday, September 7, 2022

Try echo count($task_array['task']);

In general, if you wonder what the structure of the value of a variable $var is, do a

<pre><?php var_export($var, true); ?></pre>

Advantage of this function over alternatives such as serialize and print_r is, that it prints PHP code (and is thus readable by anyone who understands PHP (which is likely if you program in PHP)). Disadvantage of var_export is, that it cannot handle circular structures (e.g. if $a->b == $a), but neither can JSON.

Sunday, September 11, 2022

Once you've retrieved the response from the API (using AsyncTask), take the response text and create a JSONArray. Then you can loop through the array and extract the categories.

public void onApiResponse(String response) {
    // Create a JSONArray from the response
    JSONArray jsonArray = new JSONArray(response);

    // Create a structure to store all of the categories
    List<Category> categories = new ArrayList<Category>();

    // Loop through the array and parse all the categories
    for (int i = 0; i < jsonArray.length(); i++) {
        // Extract the JSONObject from the index
        JSONObject jsonCategory = jsonArray.getJSONObject(i);

        // Add the category to the list

    // Do something with your categories...

EDIT: I see you hardcoded some values into your fromJSON, so I thought I'd post a corrected version of that as well.

public static Category fromJSON(JSONObject jsonObject) {
    try {
        Category category = new Category();
        category.mCategoryID = jsonObject.getInt("id");
        category.mCategoryName = jsonObjectgetString("name");

        return category;
    } catch (JSONException e) {
        return null;
Saturday, September 3, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :