0

I'm getting mad because of this. I'm using Parse.com to handle my Backend and I've to call a Cloud Code function giving this "structure":

{ "idShop":"asdf", "idCostumer":"zxcv", "selection": [
    {"idOffer":"product1", "quantity":3 }, 
    {"idOffer":"product2", "quantity":1 } 
] }

It works fine if I use curl but, I've no idea how to do it with Android. So, the idea is put an array into a JSON value.

I've tried to put all the info into a String and into a HashMap array and I got no result.

Any idea??

Thanks for helping! :)


After Sunil answer:

This is what I'm doing and doesn't work:

HashMap<String, String [] > params = new HashMap<String, String []>();
                params.put("idCostumer", new String[]{costumerId});
                params.put("idShop", new String[]{idShop});


                OfferListItem item;
                ArrayList<String> selectionList = new ArrayList<String>();

                for(int i=0; i<offerList.size();i++){                   
                    item = offerList.get(i);
                    if (item.getClicks()>0){    
                        selectionList.add("\"idOffer\":"+item.getId()+", "+"\"quantity\":"+item.getClicks());                       
                    }
                }
                String [] selection = new String [selectionList.size()];
                for (int i=0;i<selection.length;i++) {
                    selection[i]=selectionList.get(i);
                }

                params.put("selection", selection);

3 Answers 3

2

According to Parse's Android Guide, the correct data type to use for arrays is JSONArray. Try using JSONArray in place of String [].

Sign up to request clarification or add additional context in comments.

Comments

1

try this way, put the String array into hashmap with this way

 HashMap subjects = new HashMap<String, String[]>();
    subjects.put("calculus",new String[] {"math","logic"});
    subjects.put("chemisty",new String[] {"ions","electrons"});
    subjects.put("biology",new String[] {"life","bacteria"});
    for(String s:subjects.get("biology")){
        System.out.println(s);
    }

1 Comment

Sorry, doesn't work it puts @[String:25161] (or similar). It puts the direction, not the object.
1

This might be late but try this . In your cloud code, use use 2 array variables. The first will be for building your array objects and the second for building an 'ArrayList' so assuming you want to return an arraylist of courses and the mark 3 students got in each you will end up with something like this.

-Cloud code

studentMarks={};

courses=[ ];

For(int i=0;i<3;i++)

{

studentMarks={stud1:3,stud2:10,stud3:45}

courses.push(studentMarks);

}

response.success(courses)

-Then in your Android code pass an ArrayList of type Object

public void done(ArrayList<Object> courses, ParseException e) {

// cast the course to a HashMap

HashMap<String,Object> object= (HashMap<String,Object>)courses.get(0);

int stud1 = (Integer) object.get("stud1");                      

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.