Just for fun, I thought I'd ask how many different ways there are of doing this. Not syntactically, but how many different libraries can be used.
Here's a really simple JSON string:
{ "name": "bob", "email": "bob.bob.bob@bobaran.com", "phone": "555-262-6726", "zip": "12345" }
Don't hand-optimize it knowing what's there. Use the parser and split it up using common practices for data that could have a dozen or more input fields with different values.
Take this as the input string and list the three values in this format and order (so you have to seach for the name, not just spit them out in order):
zip = 12345
name = bob
phone = 555-262-6726
email = bob.bob.bob@bobaran.com
Different variations for a library would be interesting to see as well.
Make sure it compiles and runs.
I'm asking because I think I'm running into some issues where things from different libraries are interfering with each other. But aside from this, it's a simple thing but it's hard to find all of the different ways to do it.
Using JavaScript
// JSON string
const jsonString = '{ "name": "bob", "email": "bob.bob.bob@bobaran.com", "phone": "555-262-6726", "zip": "12345" }';
// Parse the JSON string into an object
const data = JSON.parse(jsonString);
// Access values
const name = data.name;
const email = data.email;
const phone = data.phone;
const zip = data.zip;
console.log(`Name: ${name}`);
console.log(`Email: ${email}`);
console.log(`Phone: ${phone}`);
console.log(`Zip: ${zip}`);
Using Delphi (and unit WEBLib.JSON):
var
JSONString: String;
JSONObject: TJSONObject;
JSONValue: TJSONValue;
begin
// JSON string
JSONString := '{"name": "bob", "email": "bob.bob.bob@bobaran.com", "phone": "555-262-6726", "zip": "12345"}';
// Parse the JSON string into a JSON object
JSONObject := TJSONObject.ParseJSONValue(JSONString) as TJSONObject;
try
// Access values
JSONValue := JSONObject.GetValue('name');
if JSONValue <> nil then
console.log('Name: ', JSONValue.Value);
JSONValue := JSONObject.GetValue('email');
if JSONValue <> nil then
console.log('Email: ', JSONValue.Value);
JSONValue := JSONObject.GetValue('phone');
if JSONValue <> nil then
console.log('Phone: ', JSONValue.Value);
JSONValue := JSONObject.GetValue('zip');
if JSONValue <> nil then
console.log('Zip: ', JSONValue.Value);
finally
JSONObject.Free;
end;
end;