Loading ...
Sorry, an error occurred while loading the content.

Ordered JSONObject?

Expand Messages
  • sco1326
    I need to create a JSON object using the Java json.org API in the format of the following example: { test :{ 97523 :{ attrs :{}, name :{ rec :41523}},
    Message 1 of 3 , Jun 26, 2006
    • 0 Attachment
      I need to create a JSON object using the Java json.org API in the
      format of the following example:

      {"test":{"97523":{"attrs":{},"name":{"rec":41523}},
      "654712":{"attrs":{},"name":{"rec":106512}},
      "3895608":{"attrs":{},"name":{"rec":238908}}},
      "code":"OK"}

      It is very useful in javascript to be able to directly access specific
      rows in this manner:
      x.test[97523].name.rec

      The problem is I need the named array values to be in a sorted order.
      The JSONObject is very clear about it being an unordered collection.
      Is there any way to maintain this structure and return an ordered
      object using the Java json.org objects?

      Any suggestions on how to achieve these goals or something similar?

      Thanks
    • Fang Yidong
      use JSONArray: { test :[ 97523 :{ attrs :{}, name :{ rec :41523}}, 654712 :{ attrs :{}, name :{ rec :106512}}, 3895608 :{ attrs :{}, name :{ rec :238908}}],
      Message 2 of 3 , Jun 26, 2006
      • 0 Attachment
        use JSONArray:

        {"test":["97523":{"attrs":{},"name":{"rec":41523}},
        "654712":{"attrs":{},"name":{"rec":106512}},"3895608":{"attrs":{},"name":{"rec":238908}}],
        "code":"OK"}


        --- sco1326 <sco1326@...>:

        > I need to create a JSON object using the Java
        > json.org API in the
        > format of the following example:
        >
        > {"test":{"97523":{"attrs":{},"name":{"rec":41523}},
        >
        > "654712":{"attrs":{},"name":{"rec":106512}},
        >
        > "3895608":{"attrs":{},"name":{"rec":238908}}},
        > "code":"OK"}
        >
        > It is very useful in javascript to be able to
        > directly access specific
        > rows in this manner:
        > x.test[97523].name.rec
        >
        > The problem is I need the named array values to be
        > in a sorted order.
        > The JSONObject is very clear about it being an
        > unordered collection.
        > Is there any way to maintain this structure and
        > return an ordered
        > object using the Java json.org objects?
        >
        > Any suggestions on how to achieve these goals or
        > something similar?
        >
        > Thanks
        >
        >
        >
        >



        --
        JSON: Action in AJAX!

        JSON - http://www.json.org
        JSON.simple - http://www.json.org/java/simple.txt




        ___________________________________________________________
        抢注雅虎免费邮箱-3.5G容量,20M附件!
        http://cn.mail.yahoo.com
      • Bakul L Patel
        Well, if you dig API little, you would come to know that it uses HashMap that does not keep that element in any order. May be you can use LinkedHashMap for
        Message 3 of 3 , Jun 27, 2006
        • 0 Attachment
          Well, if you dig API little, you would come to know that it uses HashMap that does not keep that element in any order.
          May be you can use LinkedHashMap for your purpose. But for that you need to change the API that you have got from json.org.  


          On Tue, 27 Jun 2006 sco1326 wrote :
          >I need to create a JSON object using the Java json.org API in the
          >format of the following example:
          >
          >{"test":{"97523":{"attrs":{},"name":{"rec":41523}},
          > "654712":{"attrs":{},"name":{"rec":106512}},
          > "3895608":{"attrs":{},"name":{"rec":238908}}},
          > "code":"OK"}
          >
          >It is very useful in javascript to be able to directly access specific
          >rows in this manner:
          >x.test[97523].name.rec
          >
          >The problem is I need the named array values to be in a sorted order.
          > The JSONObject is very clear about it being an unordered collection.
          > Is there any way to maintain this structure and return an ordered
          >object using the Java json.org objects?
          >
          >Any suggestions on how to achieve these goals or something similar?
          >
          >Thanks
          >
          >
          >


          [Non-text portions of this message have been removed]
        Your message has been successfully submitted and would be delivered to recipients shortly.