code is:
HashMap<String, String> map = new HashMap<>();
JSONObject jsonResponse = new JSONObject(response);
HashMap<String, String> question1 = readJsonObject(jsonResponse.getJSONObject("1"), map);
HashMap<String, String> question2 = readJsonObject(jsonResponse.getJSONObject("2"), map);
HashMap<String, String> question3 = readJsonObject(jsonResponse.getJSONObject("3"), map);
q1.setText(question1.get("question")); //q1,q2,q3 are textviews
q2.setText(question2.get("question"));
q3.setText(question3.get("question"));
this is readJsonObject method/function:
private HashMap<String,String> readJsonObject(JSONObject jsonObject,HashMap<String, String> map ) throws JSONException {
map.put("question", jsonObject.getString("question"));
map.put("optiona", jsonObject.getString("optiona"));
map.put("optionb", jsonObject.getString("optionb"));
map.put("optionc", jsonObject.getString("optionc"));
map.put("optiond", jsonObject.getString("optiond"));
map.put("correct", jsonObject.getString("correct"));
return map;
}
but i am getting same string value in q1 q2 q4 TextViews. please solve this.
response getting from the server:
{
"0":[],
"success":true,
"1":{ "question":"question3",
"optiona":"optiona",
"optionb":"optionb",
"optionc":"optionc",
"optiond":"optiond",
"correct":"optiona"},
"2":{ "question":"question1",
"optiona":"optiona",
"optionb":"optionb",
"optionc":"optionc",
"optiond":"optiond",
"correct":"optiond"},
"3":{ "question":"question4",
"optiona":"optiona",
"optionb":"optionb",
"optionc":"optionc",
"optiond":"optiond",
"correct":"optionc"}
}
getting different value from server in response but q1,q2,q3 showing same values. why? please solve this
if i am adding map.clear() after every readJsonObject() textview becomes blank,,,,, and if i add map.clear in readJsonObject() no change still same value in q1,q2,q3
if i use different hashmap in every time calling readJsonObject getting right answer but i think it is not efficient. right?
Change the method to this
private HashMap<String,String> readJsonObject(JSONObject jsonObject ) throws JSOException {
HashMap<String, String> tempMap = new HashMap<>();
tempMap.put("question", jsonObject.getString("question"));
tempMap.put("optiona", jsonObject.getString("optiona"));
tempMap.put("optionb", jsonObject.getString("optionb"));
tempMap.put("optionc", jsonObject.getString("optionc"));
tempMap.put("optiond", jsonObject.getString("optiond"));
tempMap.put("correct", jsonObject.getString("correct"));
return tempMap;
}
Related
im trying to mimic this structure to post data to mailchimp, im using volley
{
"email_address": "testchimp#gmail.com",
"status": "subscribed",
"merge_fields": {
"FNAME": "testmailchimp",
"LNAME": "testingit",
}
}
i have done this, but i need to add the FNAME and LNAME and i dont know how to do it right
#Override
protected Map<String,String> getParams(){
Map<String,String> params = new HashMap<String, String>();
params.put("email_address","testchimp#gmail.com");
params.put("status","unsubscribed");
//here i want to add the arraylist FNAME and LNAME
return params;
}
Solved my problem with this
final JSONObject jsonBody = new JSONObject("{\"email_address\":\"mailchimptester#gmail.com\"," +
"\"status\":\"unsubscribed\"," +
"\"merge_fields\":{\"FNAME\":\"test\",\"LNAME\":\"teste\"}}");
So with this i can do the json how i want without maps and then validate it here https://jsonlint.com/
It's not an ArrayList it would be a Map. You can do it using this code
HashMap<String,String> merge_fields = new HashMap<>();
merge_fields.put("FNAME","testmailchimp");
merge_fields.put("LNAME","testingit");
String merge_fields_param = new JSONObject(merge_fields).toString();
params.put("merge_fields",merge_fields_param);
I have a LinkedHashMap of contacts. For each contact I save inside the LinkedHashMap , a new LinkedHashMap with a variable amount of results of an Action. For example,
Contact 1(Key)
Action1(Key) - 2/10(Value)
Action2 - 3/10
.....
Action10 - 3/5
Contact 2
Action1 - 2/10
Action2 - 3/10
Action3 - 4/3
How I am going to put them in the following order like:
Action1 Action2 Action3 ..... Action10
Contact1 result
Contact2
To display them in gridview or tablelayout or webview?
I am trying with following to access elements but I get all of them in the arraylist
ArrayList Valuestrings = new ArrayList();
ArrayList KValuestrings = new ArrayList();
for (Map.Entry<String, LinkedHashMap<String, String>> entry : LnkHshPlayerData.entrySet())
{
for (Map.Entry<String, String> entry2 : entry.getValue().entrySet())
{
KValuestrings.add(entry2.getKey());
Valuestrings.add(entry2.getValue());
}
}
i'm not sure it's what you ask for but if you do:
ArrayList strings = new ArrayList();
HashMap<String, HashMap<String, String>> selects = new HashMap<String, HashMap<String, String>>();
for (Map.Entry<String, HashMap<String, String>> entry : selects.entrySet()) {
for (Map.Entry<String, String> entry2 : entry.getValue().entrySet()) {
strings.add(entry2.getValue());
}
}
gridViewAdapter.addAll(strings);
I want to pass an array of the same element, but i want them to be passed as new fields. I want to do something like:
#POST("/api/userInfo/changeProfileData/")
void updateProfile(
#Field("userInfoProfile[languages][0]") String language1,
#Field("userInfoProfile[languages][1]") String language2,
Callback<BaseModel<StateModel>> callback);
But the problem is that i don't know the number of elements so i cannot do like above.
I tried to pass a list:
#Field("userInfoProfile[languages]") List<String> languages
but the value is not valid because i need fields with their index (userInfoProfile[languages][0]).
Use FieldMap to make field association more dynamic.
Update your interface to:
void updateProfile(
#FieldMap Map<String, String> languages,
Callback<BaseModel<StateModel>> callback);
Method that will build FieldMap out of your list
private Map<String, String> buildFieldMap(List<String> data){
Map<String, String> map = new HashMap<String, String>();
String fieldName = "userInfoProfile[languages][%d]"
for(int i = 0; i < data.size(); i++){
map.put(String.format(fieldName, i), data.get(i));
}
return map;
}
In Xamarin, I am trying this tutorial: http://www.javacodegeeks.com/2013/06/android-listview-tutorial-and-basic-example.html
Here is the code:
List<Map<String, String>> planetsList = new ArrayList<Map<String,String>>();
private HashMap<String, String> createPlanet(String key, String name) {
HashMap<String, String> planet = new HashMap<String, String>();
planet.Put(key, name);
return planet;
}
May I please have some help with the correct using statements for the following types:
Map
ArrayList
HashMap
Thanks in advance
Map and ArrayMap are in java.util. ArrayList is in System.Collections.
I've created a JSON encode where you enter a HashTable (public Hashtable<?, ?> JSonDecode(String data) {... return objJS.toString(); } ) and get a string in JSON format. That is:
If I have a Hashtable with this (Hashtable in Hashtable):
Example Hashtable:
Hashtable<String, Object> exampleHT = new Hashtable<String, Object>();
exampleHT.put("Color", "Red");
exampleHT.put("OtherKey", "OtherValue");
exampleHT.put("OtherKey2", "OtherValue2");
Hashtable<String, Object> country = new Hashtable<String, Object>();
country.put("Spain", "Madrid");
country.put("France","Paris");
country.put("Italy", "Rome");
Hashtable<String, String> pokemon = new Hashtable<String, String>();
pokemon.put("Pikachu", "Electric");
pokemon.put("Charmander","Fire");
country.put("Pokemons", pokemon);
exampleHT.put("Countries", country);
I use my function(JSonEncode(exampleHT);) and I get this string:
{
"Color":"Red",
"Countries":{
"Spain":"Madrid",
"France":"Paris",
"Italy":"Rome",
"Pokemons":{
"Pikachu":"Electric",
"Charmander":"Fire"
}
},
"OtherKey":"OtherValue",
"OtherKey2":"OtherValue2"
}
It works perfectly! My problem is to create the reverse process, with JSonDecode.
Hashtable<?, ?> hashUnknown = JSonDecode(jsonStringExample);
public Hashtable<?, ?> JSonDecode(String data) {
// I do not know how to parse json in Hashtable, without indicating the tags manually.
}
I do not know how to parse json in Hashtable, without indicating the tags manually.
That is, without it:
JSONArray menuObject = new JSONArray (jObject.getString ("Color"));
JSONArray menuObject = new JSONArray (jObject.getString ("Countries"));
This should be dynamic without knowing json content without writing manually Color, Countries, ....
Any ideas or advice? Thanks,
You can get an Iterator object (java.util.Iterator) over the keys of your JSONObject (jObject)
So you can write something like this:
Iterator<String> it = jObject.keys();
String key = null;
Object value = null;
while (it.hasNext()) {
key = it.next();
value = jObject.get(key);
// Then test the instance of the value variable
// and perform some logic
}