I have a field type JsonObject in Baqend. How can I save data with it's SDK ?
you have to set the field to your JsonObject.
For example:
let obj = new DB.Test();
obj.json = { "foo": "bar" };
obj.save();
Related
How can I iterate a json object and get it's values in dart.
Below is an Example json object that is want to get it's value.
"SomeJsonList": {
"image": ["imageUrl1", "imageUrl2"],
"video": ["videoUrl1", "videoUrl2"]
}
To get the values of the image and video object as a list.
The output i am looking for:
var urlList = [];
urlList = [imageUrl1, imageUrl2,videoUrl1,
videoUrl2]
You can merge list items using addAll as below
void main() {
Map<String, List<String>> json = {
"image": ["imageUrl1", "imageUrl2"],
"video": ["videoUrl1", "videoUrl2"]
};
List<String>? img = json["image"];
List<String>? vid = json["video"];
List<String> list = [];
list.addAll(img!); // better check if `img` not null instead of "!"
list.addAll(vid!); // better check if `vid` not null instead of "!"
// list.addAll(xyz); // some more if needed
print(list); // [imageUrl1, imageUrl2, videoUrl1, videoUrl2]
}
I'm struggling to get the string values from a Map<string, List<string>> object. The object contains this information:
{"thisKeyName": [Array]}
The [Array] contains something like this:
["Contact 1", "Contact 2", "Contact 3"]
I'm able to access the [Array] values of the object like this:
let contactCards = currentUser.contacts; // <- this is the Map object
const ids = [];
contactCards.forEach(folder => {
folder.forEach(contact => {
ids.push(contacts);
});
});
However, I want to access the "ThisKeyName" value to put it on another array. How can I achieve this?
I have a query, I have to read key "class" under key"studentData" from the below mentioned json response.
{
"studentData": [
{
"name": "john",
"class": 2,
"rollno": "2015"
}
],
"yearofenrollment": 2017
}
Please help.
Thanks in advance
JSONObject jsonObjectResponse = new JSONObject(response.toString()); //obtain the object
JSONArray jsonMainNode = jsonObjectResponse.optJSONArray("studentData");//get array from object
JSONObject jsonChildNode = jsonMainNode.getJSONObject(0);//get first object in array
String studentDataValue = jsonChildNode.optString("class");//obtain value from class key
I need to get the inner object value in localStorage.i.e object inside the object.
var filter = {
filterWord: null,
userId: null
}
filter.filterWord = listCAO.sortName;
filter.userId = listCAO.currentUser;
listCAO.filterBreadcumText.push(filter);
localStorage.setItem('entityBreadCumText', listCAO.filterBreadcumText);
LocalStorage only holds String pairs: 'string1'='string2'
So when you do localStorage.getItem('string1') it returns 'string2'.
If you want to store a Javascript Object, you need to convert it into a string first. JSON works best for that.
var myObj = [{'name': 'Paul', 'age': 22}, {'name': 'Steve', 'age': 68}];
myStr = JSON.stringify(myObj);
localStorage.setItem('myData', myStr);
Same when you read the data from localStorage
var myStr = localStorage.getItem('myData');
var myObj = JSON.parse(myStr);
var myName = myObj[0].name;
Or in one step
var myName = JSON.parse(localStorage.getItem('myData'))[0].name;
This may be another solution.
You can use it this way.
let obj = JSON.parse(localStorage.getItem('your_settings_name'));
let lobj: YourObject = <YourObject>obj;
If the data is stored as nested objects instead of an array as c14l 's answer, the syntax changes a little bit.
Let's store nested object first:
var myNestedObject = {"token": "Bearer", "profile": {"name":"Mustafa","expires_at":1678013824}};
myNestedStr = JSON.stringify(myNestedObject);
localStorage.setItem('myNestedData', myNestedStr);
Now let's see how to get the "name" from the nested object:
var nestedStr = localStorage.getItem('myNestedData');
var nestedObj = JSON.parse(nestedStr);
var nestedProfile = nestedObj.profile;
var nestedName = nestedProfile.name;
Or we can get "name" with a single line also:
var nestedNameWithOneLine = JSON.parse(localStorage.getItem('myNestedData')).profile.name;
I have a problem with the construction of a Json for Amazon Kinesis.
This json has to have this format:
{
"Records": [
{
"Data": "XzxkYXRhPl8x",
"PartitionKey": "partitionKey1"
},
{
"Data": "f1PxFQo92Afh",
"PartitionKey": "partitionKey2"
},
{
"Data": "Gi4sEdd08HypA",
"PartitionKey": "partitionKey3"
}
],
"StreamName": "exampleStreamName"
}
I use an BeanShell Sampler to create the json as a buffer:
import org.json.JSONArray;
import org.json.JSONObject;
//Dichiarazione variabili
int timestampValue=(${startTime}+${i}+1);
float current_powerValue=${current_power_1}+${__Random(0,10)};
String idValue=${__threadNum}+"_"+"5";
JSONObject part = new JSONObject();
//Create JSON
part.put("timestamp",timestampValue);
part.put("parent","${__threadNum}");
part.put("id",idValue);
part.put("state","on");
part.put("today_kwh",65);
part.put("current_power",current_powerValue);
part.put("today_on_time",0);
part.put("on_for",0);
part.put("today_standby_time",0);
//ADD json to array
if(${i}%(${bufferSize}*${sample}-1)==0 && ${i}!=0 || ${i}==${totalNumber}-${endOfDb}){
//Add to json variable the last json created
vars.put("json",vars.get("json")+part.toString());
//Make an JSONObject by json variable of jmeter
JSONObject tempArray= new JSONObject(vars.get("json"));
log.info(tempArray.toString());
//Add tempArray into JSONArray so that it adds square brackets
JSONArray records= new JSONArray();
records.put(tempArray);
//Add the second field streamName
JSONObject kinesis = new JSONObject();
kinesis.put("records",records);
kinesis.put("streamName","kinesis");
//save into jsonBuffer
vars.put("jsonBuffer",kinesis.toString());
//restart json variable
vars.put("json","");
}
else{
//add new json into variable so to store it.
vars.put("json", vars.get("json")+part.toString()+",");
}
I use json variable in jmeter to save the json for each iteraction and when the "i" variable respect the if clause then I start to create the json structure.
So I add the last json to the jmeter variable, then I create a JSONObject to store this json but when i do this it store only one json (because it is an object).
Unfortunately if I store in a JSONArray it add "" because read the variable json as a string.
The best solution would be use only JSONObject and JSONArray but how I use the same object for all the iteractions(in jmeter i can't use JSONArray)
This is my jmx
You could trt with this snippet:
if(${i}%(${bufferSize}*${sample}-1)==0 && ${i}!=0 || ${i}==${totalNumber}-${endOfDb}){
vars.put("json",vars.get("json")+part.toString());
JSONArray records= new JSONArray("["+vars.get("json")+"]");
log.info(records.toString());
//records.put(tempArray);
JSONObject kinesis = new JSONObject();
kinesis.put("records",records);
kinesis.put("streamName","kinesis");
vars.put("jsonBuffer",kinesis.toString());
vars.put("json","");
}