ParseObject.put method saving strings etc as arrays? Is this a bug? - android

I try to save simple Object attibutes using the .put("name", data) method
ParseObject parseLibraryItem = new ParseObject("UserLibrary");
parseLibraryItem.add("movieId", 121);
parseLibraryItem.add("runtime", 134);
parseLibraryItem.add("status", "released");
parseLibraryItem.add("name", "TestParseMovie");
parseLibraryItem.add("releaseDate", "2014-11-06");
parseLibraryItem.add("imagePath", "/7k9db7pJyTaVbz3G4eshGltivR1.jpg");
parseLibraryItem.add("description", "This is just a test");
parseLibraryItem.add("tagline", "The craziest movie ever");
parseLibraryItem.add("createdBy", ParseUser.getCurrentUser());
parseLibraryItem.setACL(new ParseACL(ParseUser.getCurrentUser()));
parseLibraryItem.saveInBackground();
Here is the Parse backend
When I try to query the Object I have to then strip the returned string of the Brackets. What am I doing wrong here?

Instead of using the method add(String key, Object value), which atomically adds an object to the end of the array associated with a given key, try using the method put(String key, Object value), which adds a key-value pair to this object.
source : Android Parse API https://parse.com/docs/android/api/

Related

Compare object to object from hash map in android

I have HashMap<Shop, String> shopMap where I put two values:
shopMap(shopModel1, shopModel1.getName());
shopMap(shopModel2, shopModel2.getName());
In my method for search shop by name I passed object of shop identical like shopModel1 to get his name:
public String getNameForShop(Shop filter) {
return shopMap.get(filter);
}
but I get null. Objects have the same all values. There is any way to get shop name from hash map using object?
It is not ok to have the object as key value. Map it by name.
I guess mapping by object, in fact, it happens with the object's memory address. Then you search in map for an object it doesn't compare objects fields, just the addresses.
HashMap<String, Shop> shopMap;
if( shopMap.get("shopname").equals(anotherShop) ){
//do staff
}

Java - Parse - iterate over ParseObject fields

Having a ParseObject object how can I loop through its fields and get the name of the field along with the value of it? This would really help me minimize my code.
Hmm, ParseObject contains key-value pairs, and I think you can't iterate though it. But. I found something called .keySet() method of ParseObject. It returns ... well, the set of keys (excluding createdAt, updatedAt, authData, or objectId). I think you can convert it into an array and iterate trhough it?
Something like this:
Set<String> keySet = parseObject.keySet();
String[] parseKeys = keySet.toArray(new String[keySet.size()]);
for (String key : parseKeys) {
String parseValue = parseObject.get(key);
//do whatever you want
}

com.loopj.android.http.RequestParams Don't works with put(String key, Object value)

RequestParams have put Object method,put(String key, Object value).
so I use the code below; On the server, the userId cann't find in get or post paramenters.
com.loopj.android.http.RequestParams params= new RequestParams ();
//添加参数 add params
params.put("userId", userId); //Long userId;
params.put("nickname", nickname);//String
try using params.put("userId", Long.toString(userId));
if you look at the source code of the library, you will see that method put(String key,Object value) adds param with non-string value (e.g. Map, List, Set). you will also find some recently added methods which accept long or int as value, but if you are using the library jar file, version 1.4.4, you will notice that you haven't those new methods thus you can use put(String,String) to get it work

JSON string not in order

I want the JSON string to be in same order how I am putting it.This is my query.
object.put("name", name);
object.put("email", email);
object.put("query", query);
But in the resultant string its showing as
{"email""m#gmail.com","query":"k","name":"a"}
The order of keys in a JS object is not guaranteed. If you need a particular order, consider having a separate array of keys to preserve the ordering.
{
"order":["name", "email", "query"],
"data":{
"email":"m#gmail.com",
"query":"k",
"name":"a"
}
}
From JSON specification http://www.ietf.org/rfc/rfc4627.txt:
An object is an unordered collection of zero or more name/value pairs, where a name is a string and a value is a string, number, boolean, null, object, or array.
(emphasis mine)

Json.put() function puts double quotes automatically

I am preparing a json object to be sent over a webservice , I am trying to put a list of String in the object, Something like this:
["24348f08-92f4-481a-9a36-ed0d533ca4f3", "24348f08-92f4-481a-9a36-ed0d533ca4f3"]
What i have done:
sendData.put("SpecializationAlert",Specialization);
sendData is a json object and Specialization is a String array, the result when i log this is:
"[\"24348f08-92f4-481a-9a36-ed0d533ca4f3\",\"24348f08-92f4-481a-9a36-ed0d533ca4f3\"]"
Specialization is put in the JSON as a toString()-ed object. You may create a JSONArray from it first and then include it in the JSONObject:
sendData.put("SpecializationAlert",new JSONArray(Arrays.asList(Specialization)));

Categories

Resources