jiduvah
jiduvah

Reputation: 5168

Java Equivilent of a PHP Array

I need to send an array to a server but I cannot seem to find the correct formatting for it.

Here is the php equivalent that I need to send

array(
      'interest' => 1,
      'charity' => 590,
      'items' => array(
        array(
          'cart_item_id' => 11197,
          'message' => '',
          'aid' => 174
        ),
      ),
    );

I am trying to get send my data in nameValuePairs - here is what I am trying

List<NameValuePair> nameValuePairs  =   new ArrayList<NameValuePair>();
    if (interest == true) {
        nameValuePairs.add(new BasicNameValuePair("interest", "1"));                
    } else {
        nameValuePairs.add(new BasicNameValuePair("interest", "0"));    
    }
    nameValuePairs.add(new BasicNameValuePair("charity", String.valueOf(charityId)));
    int len = cartItems.size();
    for (int i = 0; i < len; ++i) {
         nameValuePairs.add(new BasicNameValuePair("items[" + String.valueOf(i) + "]" + "[cart_item_id]", String.valueOf(cartItems.get(i).cartItemId)));
        nameValuePairs.add(new BasicNameValuePair("items[" + String.valueOf(i) + "]" + "[message]", cartItems.get(i).message));
        nameValuePairs.add(new BasicNameValuePair("items[" + String.valueOf(i) + "]" + "[aid]", String.valueOf(cartItems.get(i).addressId)));
    }

This is incorrect tho. As always any help is much appreciated

Edit

For clarification this is what my app sends to the server

[interest=0, charity=1878, items[0][cart_item_id]=14498, items[0][message]=, items[0][aid]=315, items[1][cart_item_id]=14499, items[1][message]=, items[1][aid]=318]

but that is incorrect

Upvotes: 0

Views: 107

Answers (3)

Houcine
Houcine

Reputation: 24181

use a HashMap<Object,Object> , and then when you want to add the sub array :

array(
          'cart_item_id' => 11197,
          'message' => '',
          'aid' => 174
)

just add a new HashMap<String,String> into your Super HashMap<Object,Object> : the logic will be something like this:

HashMap parentMap = new HashMap(); HashMap subMap = new HashMap();

// put params into parentMap
parentMap.put("key1", "this is a string value");
parentMap.put("key2", 45);
parentMap.put("key3", new Date());

subMap.put("card_item_id", ""+11197);
subMap.put("message", "this is a message from subMap");
parentMap.put("items", subMap);

//get the subMap from the parentMap like this 
HashMap<String, String> copySubMap = (HashMap<String, String>) parentMap.get("items");

hope that you got the idea of how formatting your array :)

Upvotes: 1

Aldo Stracquadanio
Aldo Stracquadanio

Reputation: 6237

You should use the HashMap in the package java.util. Have a look to the API docs for more details ;)

Upvotes: 2

jamesTheProgrammer
jamesTheProgrammer

Reputation: 1777

Java class HashMap should do it. Uses key-value pair concept.

Here is implementation example, http://www.javadeveloper.co.in/java-example/java-hashmap-example.html

Upvotes: 0

Related Questions