JavaScript Object Notation (JSON)


JavaScript Object Notation

It is a lightweight format to store and exchange data. A JSON document may store: a JSON object, a JSON array, a string, a number, null, true or false.
Este es un formato simple para almacenar e intercambiar datos. Un documento de JSON puede almacenar: a objeto de JSON, un arreglo de JSON, una cadena de caracteres (texto), un número, null, true o false.

value

SysJson

JSON Object

An object is an unordered set of pairs. Each pair has a name and a value. An object begins with a opening curly bracket and ends with a closing curly bracket as shown below.
Un objeto es un conjunto de pares que no están ordenados. Cada par tiene un nombre y un valor. Un objeto comienza con una llave de abrir y termina con una llave de cerrar como se muestra debajo.

Object

JSON Array

It is an ordered collection of values. An array begins with an opening bracket and ends with a closing bracket as shown below.
Este es una colección ordenada de valores. Un arreglo comienza con un paréntesis cuadrado de abrir y termina con un paréntesis cuadrado de cerrar como se muestra debajo.

Array

Problem 1
Create a Wintempla Dialog application called Electronics to test the JSON format. Use Notepad to create the Resistor.json file as shown, save the file using UTF-8 enconding. Save the file in your project folder. If you save the file somewhere else, you will need to specify the file location in the program. Observe that the pairs in a JSON object are not ordered.
Cree una aplicación de Diálogo de Wintempla llamada Electronics para probar el formato JSON. Use el Block de Notas para crear el archivo Resistor.json como se muestra, guarde el archivo usando la codificación UTF-8. Guarde el archivo en la carpeta de su proyecto. Si usted guarda el archivo en otro lugar, usted tendrá que especificar la ubicación del archivo en el programa. Observe que los pares en un objeto JSON no están ordenados.

ResistorJson

ProjectFolder

ElectronicsRun

Electronics.h
#pragma once //______________________________________ Electronics.h
#include "Resource.h"

class Electronics: public Win::Dialog
{
public:
     Electronics()
     {
     }
     ~Electronics()
     {
     }
     wstring text;
     void DisplayJson(Sys::Json& json);
protected:
     ...
};


Electronics.cpp
...
void Electronics::Window_Open(Win::Event& e)
{
     Sys::Json json;
     const wchar_t* error = json.Load(L"resistor.json");
     if (error != NULL)
     {
          this->MessageBox(error, L"Electronics", MB_OK | MB_ICONERROR);
     }
     DisplayJson(json);
     tbxOutput.Text = text;
}

void Electronics::DisplayJson(Sys::Json& json)
{
     if (json.Type == JSON_TYPE_STRING)
     {
          text += L" \"";
          if (json.StringValue != NULL) text += json.StringValue;
          text += L"\"\r\n";
     }
     else if (json.Type == JSON_TYPE_NUMBER)
     {
          wchar_t tmp[64];
          if (json.isIntegerNumber)
          {
               _snwprintf_s(tmp, 64, _TRUNCATE, L"%I64i ", json.NumberValue);
          }
          else
          {
               _snwprintf_s(tmp, 64, _TRUNCATE, L"%g ", json.DoubleValue);
          }
          text += tmp;
     }
     else if (json.Type == JSON_TYPE_OBJECT)
     {
          unordered_map<wstring, Sys::Json>::iterator p;
          for (p = json.ObjectValue.begin(); p != json.ObjectValue.end(); p++)
          {
               text += L"\"";
               text += p->first;
               text += L"\" :";
               DisplayJson(p->second);
          }
     }
     else if (json.Type == JSON_TYPE_ARRAY)
     {
          vector<Sys::Json>::iterator p;
          for (p = json.ArrayValue.begin(); p != json.ArrayValue.end(); p++)
          {
               DisplayJson(*p);
               text += L" \r\n";
          }
     }
     else if (json.Type == JSON_TYPE_TRUE)
     {
          text += L" true\r\n";
     }
     else if (json.Type == JSON_TYPE_FALSE)
     {
          text += L" false\r\n";
     }
     else if (json.Type == JSON_TYPE_NULL)
     {
          text += L" null\r\n";
     }
}




© Copyright 2000-2021 Wintempla selo. All Rights Reserved. Jul 22 2021. Home