Announcement

Collapse
No announcement yet.

How do I test if element exists in JSON object?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    #16
    MIVA_JSON_DECODE returns 1.
    decode was successful without any error.
    Daniel Kim, Compu-Mate
    Developer

    Comment


      #17
      "Curiouser and curiouser ..." I would go ahead and serialize l.json_reponse to see what's in there. I would also look over my code very carefully for other types of errors, such as a missing quote or brace that the compiler didn't catch. I've used all these functions in my own work recently, and never had this type of problem.
      Kent Multer
      Magic Metal Productions
      http://TheMagicM.com
      * Web developer/designer
      * E-commerce and Miva
      * Author, The Official Miva Web Scripting Book -- available on-line:
      http://www.amazon.com/exec/obidos/IS...icmetalproducA

      Comment


        #18
        There is no error on JSON string since decoded any element in JSON object can be accessed.

        MIVA_STRUCT_MEMBERS does not recognize JSON array correctly.
        MIVA_ARRAY_ELEMENTS recognizes JSON array correctly.
        But, it does not distinguish between object structure and ARRAY.

        Both MIVA_STRUCT_MEMBERS and MIVA_ARRY_ELEMENTS should be used to determine ARRAY dimension correctly.
        Daniel Kim, Compu-Mate
        Developer

        Comment


          #19
          I think it's still possible that the problem is in the JSON decoding, not in miva_struct_members or miva_array_elements. Sorry to repeat myself, but that's what I would check in this case

          I'm attaching a file with a function that I use for this. (It's very small, but for some reason the forum won't let me paste it into this message.)

          EDIT: the forum won't let me attach it. You can download it at: http://themagicm.com/structure.mv

          It serializes the data, and it also URL-decodes all the values, which makes it much easier to read text that's full of quotes and other punctuation. It works with any type of data, including nested structures of arrays and objects.

          A call such as
          Code:
          <MvEVAL EXPR="{ Structure(l.json_response, '<br>') }">
          will display the contents of the variable, with one name and value on each line.
          Last edited by Kent Multer; 10-12-23, 10:09 AM.
          Kent Multer
          Magic Metal Productions
          http://TheMagicM.com
          * Web developer/designer
          * E-commerce and Miva
          * Author, The Official Miva Web Scripting Book -- available on-line:
          http://www.amazon.com/exec/obidos/IS...icmetalproducA

          Comment


            #20
            Thank you for valuable utility like this.
            I will keep in my library.

            After all these tests, I concluded that returned JSON data is in valid format.
            I was looking for a simple designator which tells me how many candidates exist in response.

            Work around is that:

            After verifying Candidate element exists,
            Check if MIVA_STRUCT_MEMBERS(candidate) returns 0 or not.
            If it returns 0, use MIVA_ARRAY_ELEMENTS to find the count
            Otherwise, it is single entity.

            Thans for your help.
            Daniel Kim, Compu-Mate
            Developer

            Comment

            Working...
            X