How to Create a Json Object

How to create JSON Object using String?

org.json.JSONArray may be what you want.

String message;
JSONObject json = new JSONObject();
json.put("name", "student");

JSONArray array = new JSONArray();
JSONObject item = new JSONObject();
item.put("information", "test");
item.put("id", 3);
item.put("name", "course1");
array.put(item);

json.put("course", array);

message = json.toString();

// message
// {"course":[{"id":3,"information":"test","name":"course1"}],"name":"student"}

Best way to create this JSON object in Golang

Declare types that match the structure of the JSON document.

type client struct {
Hostname string `json:"Hostname"`
IP string `json:"IP"`
MacAddr string `json:"MacAddr"`
}

type connection struct {
Clients []*client `json:"Clients"`
}

Initialize values using those types and encode to JSON.

var clients []*client
for _, line := range strings.Split(line, "\n") {
if line != "" {
s := strings.Split(line, " ")
clients = append(clients, &client{Hostname: s[0], IP: s[1], MacAddr: s[2]})
}
}

p, _ := json.Marshal(connection{Clients: clients})
fmt.Printf("%s\n", p)

The JSON field tags (json:"Hostname") are not needed in this example because the JSON object keys are valid exported identifiers. I include the tags here because they are often needed.

Run the code on the Go Playground.

How to create JSON object in Python

source : https://docs.python.org/3/library/json.html

import json
data = {"user2_proximity": 3, "Wifi_1": -80, "Wifi_2": -40, "Wifi_3": -40,
"thermostat": 18, "light": 0, "hour_of_day": 0, "user3_proximity": 3,
"user1_proximity": 1, "day_of_week": 1, "security": 0, "minute_of_hour": 9,
"Act_1": 1, "Act_2": 0, "Act_3": 0}

json_data = json.dumps(data)

how to create json object in swift 5

Not sure about JSON but according to giving JSON and code part.

JSON should be {"orderItems" : [{"product_id" : 19 , "quantity" : 2 , "size_key" : "39 40 42"}],"retailer_id":20,"status":"initial"}

JSON creator code:

var para : [String:Any] = [String:Any]()
var prodArray : [[String:Any]] = [[String:Any]]()

para["retailer_id"] = 20
para["initial"] = "status"

for product in colorsArray {
var prod : [String : Any] = [String : Any]()
if let productId = product.product?["id"] {
prod["product_id"] = productId
}

prod["quantity"] = "1"
prod["size_key"] = variabledata

prodArray.append(prod)
}

para["orderItems"] = prodArray
print(para)

Create JSON object dynamically via JavaScript (Without concate strings)

This is what you need!

function onGeneratedRow(columnsResult)
{
var jsonData = {};
columnsResult.forEach(function(column)
{
var columnName = column.metadata.colName;
jsonData[columnName] = column.value;
});
viewData.employees.push(jsonData);
}


Related Topics



Leave a reply



Submit