Use string concatenation.
String message = "this is value want to pass to the ActualMessage attribute " ;
String input = "{\r\n" +
"\"Level\": 0,\r\n" +
"\"Name\": \"String\",\r\n" +
"\"msgName\": \"String\",\r\n" +
"\"ActualMessage\": \"" + message + "\",\r\n" +
"\"TimeStamp\": \"/Date(-62135596800000-0000)/\"\r\n" +
"}" ;
Answer from chockleyc on Stack OverflowUse string concatenation.
String message = "this is value want to pass to the ActualMessage attribute " ;
String input = "{\r\n" +
"\"Level\": 0,\r\n" +
"\"Name\": \"String\",\r\n" +
"\"msgName\": \"String\",\r\n" +
"\"ActualMessage\": \"" + message + "\",\r\n" +
"\"TimeStamp\": \"/Date(-62135596800000-0000)/\"\r\n" +
"}" ;
How about using String.format() for this? for example, to pass a "dynamic value" declare a place holder in the text:
String input = "insert %s in the string"; // here %s is the placeholder
input = String.format(input, "value"); // replace %s with actual value
Now input will contain the string "insert value in the string". In your example, change this line:
" \"msgName\": \"String\",\r\n"
Replace it with this:
" \"msgName\": \"%s\",\r\n"
Now you can perform the substitution:
input = String.format(input, message);
Notice that the first parameter in the format() method has a lot more of options, and that you can pass more than one argument to be replaced. Take a look at the documentation for the Formatter class.
How to pass dynamic variable to json string
Passing dynamic value in Json object
How to pass dynamic parameters to a REST API from a JSON object of a structure | OutSystems
Creating a json with dynamic values - Stack Overflow
Videos
I always recommend using the serialize method to generate correct JSON:
String payload = JSON.serialize(
new Map<String, Object> {
'UserID' => username
});
This will emit the correct JSON, even if your username were to contain special characters, etc.
Your payload isn't formatted correctly. You need to wrap your dynamic string in double-quotes. These quotes won't need to be escaped, but it might be worth escaping your string to avoid any " causing the string to end prematurely.
String payload = '{"UserID":"'+username +'"}';
As far as I can understand your code, you are getting all image_file properties, even those with numbers. You can easily do it with this:
var images = [];
//iterate through the array
$.each(data, function (i, item) {
//iterate through each property
$.each(item, function (key, value) {
//if the property starts with image_file, push into the array
if (key.indexOf('image_file') === 0) images.push(value);
});
});
console.log(images); // ["path\\Off.gif","path\\Off1.gif","path\\On1.gif","path\\Off2.gif","path\\On2.gif","path\\half.gif"]
You're skipping image_file_1, and going directly from image_file to image_file_2
Try starting imageIndex at 0, and mapping 0 to image_file.
imageIndex = 0;
continueLoop = true;
while(continueLoop) {
if(imageIndex == 0) {
images.push(data[i].image_file);
}
// rest of your code unchanged
Hi,
The other day, I made a post here about parsing a txt file for a note system in my game, in which the notes were stored in an included txt file.
From there I was recommended to instead use a Json file, which I have created a struct in with keys for each note, which looks something like this:
{
"note1": "test test test",
"note2": "blah blah blah",
}To view the notes in-game, I have two objects: One note object on the floor (Obj_Note_F) which the player interacts with to view the note, and which creates the second object, which is the actual note which is displayed to the screen and has the buffer for the Json file (Obj_Note):
Obj_Note_F Step event:
if (keyboard_check_pressed(vk_enter)) && !instance_exists(Obj_Note)
{
var e = instance_create_layer(vx/3,vy/1.5,"GUI",Obj_Note,{
//All of these variables are defined in the instance variables of Obj_Note_F
sprite: noteSprite, //The sprite of Obj_Note
note: theNote, //The actual note to parse e.g note1
file: "notes.json", //The Json file
});
}When the note is created, the Json file is parsed in the create event of Obj_Note, which is as follows:
Obj_Note create event:
var buffer = buffer_load("notes.json");
var contents = buffer_read(buffer, buffer_text);
var notes = json_parse(contents);
if variable_struct_exists(notes,note)
{
text = notes.note;
}
buffer_delete(buffer);
sprite_index = sprite;However, when I try and open the note, i am thrown this error:
Variable <unknown_object>.note(100218, -2147483648) not set before reading it. at gml_Object_Obj_Note_Create_0 (line 7) - text = notes.note;
I imagine this error has something to with how the note variable is being set upon creation of Obj_Note. In the instance variable of Obj_Note_F, "theNote2 is defined as a string, e.g "note1". However, if i set "note" directly in Obj_Note as note1, it displays the text correctly. However, I cannot set it as that in the instance variable as it will throw an error as being an undefined variable.
Does anyone have a solution to this? Am I missing something completely obvious? Any help would be greatly appreciated.
Thanks.
You're looking for the range built-in.
.[] | [.field.empID = range(1;1000000)]
demo at jqplay.org
You don't need bash processing at all for this. You can use the range() function in jq to create the number range from 1 to million and create multiple objects using the reduce() function
jq -n 'reduce range(1; 1000000) as $data (.; . + [{"field": { "empID": $data, "location": "India"}}])'
This creates a million objects inside the array with empID set, starting from 1. Modify the value inside range() to customize the numbers.
What you have provided in the question is not a well formed json. Ignoring that, you can read a well formed json string into a JSONObject, and replace the values as you want :
private static String getData(String name, int age, String street, String line) throws JSONException {
JSONObject jsonObject = new JSONObject("{ name : VARIABLE1, age : VARIABLE2, address : { street : VARIABLE3, line : VARIABLE4 }}");
JSONObject address = (JSONObject) jsonObject.get("address");
jsonObject.put("name", name);
jsonObject.put("age", age);
address.put("street", street);
address.put("line", line);
return jsonObject.toString();
}
You can call this method as :
getData("Random", 20, "str", "lin");
You can use a specific Java object containing all the fields you want (with simple getters and setters) and transform it to Json using whatever library you prefer (ex. Gson, Jackson...). Alternatively, if your Json String is very simple, you can write it by hand and use String.format for replacing the variable values.
I would suggest doing it a little differently
I will try to explain in pseudo code since i dont understand your variable names
- read json file
- array = JSON.parse(fileContents)
- array.push(newItem)
- newContents = JSON.stringify(array)
- file WRITE (not append) newContents
I recommend reading the file, pushing onto an array captured from that file, and then writing the file back to disk.
Assuming the file has content already in the form of an array:
let fileDado = JSON.parse(fs.readFileSync('climatempo.json'));
fileDado.push(dado);
fs.writeFileSync('climatempo.json', JSON.stringify(fileDado));