vivazzi / JAson

Creation a JSON object with data of different types and run serialization and deserialization of JSON data.
MIT License
85 stars 46 forks source link

array of objects not work #3

Closed almp1 closed 2 years ago

almp1 commented 2 years ago

Json sample

{ "id": 54, "nome": "12.01 alterado", "condicoes": [ { "id": 47, "estrategiaId": 54, "indicadorAId": 87, "indicadorA": { "id": 87, "nome": "iATR", "parametros": [ { "id": 46, "nome": null, "valor": 10.0 } ] }, "retornoA": 0, "comparador": ">", "indicadorBId": 88, "indicadorB": { "id": 88, "nome": "iAMA", "parametros": [ { "id": 47, "nome": "Aplicar a", "valor": 1.0 }, { "id": 48, "nome": "Periodo rapido", "valor": 2.0 }, { "id": 49, "nome": "Periodo lento", "valor": 3.0 }, { "id": 50, "nome": "Deslocalmento", "valor": 4.0 }, { "id": 51, "nome": "Aplicar a", "valor": 0.0 } ] }, "retornoB": 0 }, { "id": 48, "estrategiaId": 54, "indicadorAId": 90, "indicadorA": { "id": 90, "nome": "iATR", "parametros": [ { "id": 52, "nome": null, "valor": 10.0 } ] }, "retornoA": 0, "comparador": ">", "indicadorBId": 89, "indicadorB": { "id": 89, "nome": "iAMA", "parametros": [ { "id": 53, "nome": "Aplicar a", "valor": 1.0 }, { "id": 54, "nome": "Periodo rapido", "valor": 2.0 }, { "id": 55, "nome": "Periodo lento", "valor": 3.0 }, { "id": 56, "nome": "Deslocalmento", "valor": 4.0 }, { "id": 57, "nome": "Aplicar a", "valor": 0.0 } ] }, "retornoB": 0 } ] }

I need get array of key "condicoes" "condicoes": [ { "id": 47, ... } , { "id": 48, ... }]

My code mql

string pJS=" ... " // Json Sample js.Deserialize(pJS); Print(js["nome"].ToStr()); // is correct = 12.01 alterado Print(js["condicoes"].m_type); // is correct type 6 or jtARRAY Print(js["condicoes"].Size()); //not work ... return 1, but correct 2

Can help me !?

Tanks

vivazzi commented 2 years ago

Hello, @almp1!

I added your json data in TestJAson2\test_data.json and have written simple test case (using https://github.com/vivazzi/mql_unit_test) and get true test result:

#property strict

#include <JAson.mqh>
#include <unit_test/unit_test.mqh>

class TestJAson: public TestCase {
public:

    void test() {
        CJAVal js;

        string path = "TestJAson2\\test_data.json";
        if (FileIsExist(path)){
            int h_file = FileOpen(path, FILE_READ);

            string pJS = "";
            while(!FileIsEnding(h_file)) pJS += FileReadString(h_file);

            js.Deserialize(pJS);
            assert_equal(js["condicoes"].Size(), 2);

            Print(js["nome"].ToStr());  // is correct = 12.01 alterado
            Print(js["condicoes"].m_type);  // is correct type 6 or jtARRAY
            Print("js[\"condicoes\"].Size() = " + js["condicoes"].Size());  // work! return 2 (as expected)
        }
        else Print(path + ": is not found");
    }

    void declare_tests() {
        test();
    }

};

int OnInit(){
    TestJAson test_jason;
    test_jason.run();

    return(INIT_SUCCEEDED);
}

test_result

I cannot reproduce your bug. If you get the bug, write down all your code (using code format in comment).

almp1 commented 2 years ago

Hi,

Tanks, my json string was wrong.

I make update the string and resolved.