Results 1 to 5 of 5

Thread: JSON vanuit magento uitlezen

  1. #1

    JSON vanuit magento uitlezen

    Hallo allen,

    Ik ben bezig met een koppeling met de Magento 2.0 webshop. Hier wil ik de relaties gegevens uithalen.

    Dit heb ik werkende, en ik krijg JSON data als resultaat voor het adres veld terug.

    Deze JSON data ziet er als volgt uit:
    Code:
    {"_embedded":{"addresses":
    [
      {
        "id": 2,
        "customer_id": 3,
        "region": {
          "region_code": "Nederland",
          "region": "Nederland",
          "region_id": 0
        },
        "region_id": 0,
        "country_id": "NL",
        "street": [
          "Adres met nummer"
        ],
        "company": "Bedrijf",
        "telephone": "1234567890",
        "postcode": "1234 AA",
        "city": "Plaats",
        "firstname": "Voornaam'",
        "lastname": "Achternaam",
        "default_shipping": true,
        "default_billing": true
      }
    ]  
      }
    }
    Deze lees ik vervolgens uit met onderstaande code. Dit werkt goed voor alle JSON items, behalve het adres veld(street). Dit is een array, maar hoe lees ik deze nu correct uit?

    Code:
      Try // JSON
        LJSONBytes := TEncoding.ANSI.GetBytes(sAdresJSON);
        LJSONSObj := TJSONObject.ParseJSONValue(LJSONBytes, 0);
        if LJSONSObj <> nil then begin
          try
            LJSONArray := LJSONSObj.GetValue<TJSONArray>('_embedded.addresses');
            For LJSONValue In LJSONArray Do begin
              iId         :=  LJSONValue.GetValue<integer>('id');
              iRelatieId  :=  LJSONValue.GetValue<integer>('customer_id');
              sRelatie    :=  LJSONValue.GetValue<string>('company');
              sVoornaam   :=  LJSONValue.GetValue<string>('firstname');
              sAchternaam :=  LJSONValue.GetValue<string>('lastname');
              sAdres      :=  LJSONValue.GetValue<string>('street');
              sPostcode   :=  LJSONValue.GetValue<string>('postcode');
              sPlaats     :=  LJSONValue.GetValue<string>('city');
              sLand       :=  LJSONValue.GetValue<string>('country_id');
              sTelefoon   :=  LJSONValue.GetValue<string>('telephone');
            end;
          finally
            LJSONSObj.Free;
          end; // End Try/Finally
        end; // End if LJSONSObj <> nil
      Except
        on E: Exception Do
          Begin
            Application.MessageBox(PChar('Letop! Exception opgetreden!' +#13#10+ #13#10 +
              'Fout: '+ E.ClassName +#13#10+ #13#10 + E.Message), 'Fout bij aanroepen Unit: Inlezen adressen', MB_ICONINFORMATION);
          End;
      End; // End Try JSON
    Wie kan mij in de goede richting sturen

  2. #2
    Street is, net als addresses, ook een array, dus die ook met een for loop uitlezen

  3. #3
    Dat heb ik geprobeerd, maar ik zie het even niet vrees ik.

    Met:
    Code:
     LJSONArray := LJSONSObj.GetValue<TJSONArray>('_embedded.addresses.street');
    Krijg ik de foutmelding: value _embedded.addresses.street not found.

    Heb ook

    Code:
     LJSONArray := LJSONSObj.GetValue<TJSONArray>('addresses.street');
    en
    Code:
     LJSONArray := LJSONSObj.GetValue<TJSONArray>('street');
    Geprobeerd maar geen resultaat.

  4. #4
    Hier is een voorbeeld...

    Code:
              LJSONArray := LJSONSObj.GetValue<TJSONArray>('_embedded.addresses');
              For LJSONValue In LJSONArray Do begin
                AStreet :=  LJSONValue.GetValue<TJSONArray>('street');
                For AValue In AStreet Do begin
                  ShowMessage(AValue.ToString);
                end;
              end;

  5. #5
    Dat werkt, mijn dank is groot.

    Soms zit je uren ernaar te turen en kom je niet verder....

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •