How can i get id, name, password and put it in YailList
and get 1, admin, admin123.
that is for example maybe keys length not equal id, name, password maybe more or less that is for example only, same for values
// Simple Get Json keys and values list
@SimpleFunction
public void Get(String data){
try {
JSONObject jsonObject = new JSONObject(data);
if (jsonObject.has("result")) {
JSONObject resultObject = jsonObject.getJSONObject("result");
// Get an array of keys and values
for (int i = 0; i > resultObject.length(); i++) {
}
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
OnGot();
}
});
} else {
throw new YailRuntimeError("Error Fetch Data", "Get");
}
} catch (JSONException e){
e.printStackTrace();
throw new YailRuntimeError(e.getMessage(), "Get");
}
}
@SimpleEvent
public void OnGot(YailList keys, YailList values) {
EventDispatcher.dispatchEvent(this, "OnGotRow", keys, values);
}
@SimpleFunction(description = "GetKeys")
public YailList GetKeys(String json) {
// Get the JSONObject of the key "result".
JSONObject object = new JSONObject(json);
JSONObject result = object.getJSONObject("result");
// Create a List for the output.
List<String> output = new ArrayList<String>();
// Iterate over every item in the JSONObject, and appends the keys to the list "output".
Iterator<String> keys = result.keys();
while (keys.hasNext()) {
output.add(keys.next().toString());
}
// Returns a YailList version of the output.
return YailList.makeList(output);
}
I'll leave the GetValues part to you. Hint: change something in the while loop.
Thanks @Gordon_Lu but my question is for keys and values not kays only i tried this way before i post this topic and i don't found any way to get values too.
if you know how to get values too pls tell me. thanks
@SimpleFunction(description = "Get Keys")
public YailList GetKeys(String json) {
try {
// Get the JSONObject of the key "result".
JSONObject object = new JSONObject(json);
JSONObject result = object.getJSONObject("result");
// Create a List for the output.
List<String> output = new ArrayList<>();
// Iterate over every item in the JSONObject, and appends the keys to the list "output".
Iterator<String> keys = result.keys();
while (keys.hasNext()) {
output.add(keys.next());
}
// Returns a YailList version of the output.
return YailList.makeList(output);
} catch (JSONException e) {
// Handle JSON parsing error here, you can return an empty list or handle it as needed.
e.printStackTrace();
return YailList.makeEmptyList();
}
}
@SimpleFunction(description = "Get Values")
public YailList GetValues(String json) {
try {
// Get the JSONObject of the entire input JSON.
JSONObject object = new JSONObject(json);
// Get the JSONObject of the key "result" within the input JSON.
JSONObject result = object.getJSONObject("result");
// Create a List for the output.
List<String> output = new ArrayList<>();
// Iterate over every item in the "result" JSONObject and append the values to the list "output".
Iterator<String> keys = result.keys();
while (keys.hasNext()) {
String key = keys.next();
String value = result.getString(key);
output.add(value);
}
// Returns a YailList version of the output.
return YailList.makeList(output);
} catch (JSONException e) {
// Handle JSON parsing error here, you can return an empty list or handle it as needed.
e.printStackTrace();
return YailList.makeEmptyList();
}
}