cancel
Showing results for 
Search instead for 
Did you mean: 

Help with JSON Model

0 Kudos

Hi everyone. The following works:


// Create JOSN Model Instance

var lco_JSM = new sap.ui.model.json.JSONModel();

// Create JSON

lco_JSM.setJSON(

     '{ "Data": [{"Title": "Mr.",' + '"FName": "John", "LName": "Doe"},'

          + '{"Title": "Mrs.", "FName": "Jane", ' + '"LName": "Doe"}]}');

// Bind Model To Core

sap.ui.getCore().setModel(lco_JSM);

While this doesn't work:


// Create JOSN Model Instance

var lco_JSM = new sap.ui.model.json.JSONModel();

// Sample JSON Data

var lcs_Data =

{    "Data":

    [    {    "Title" : "Mr.",

            "FName" : "John",

            "LName" : "DOe" },

         {    "Title": "Mrs.",

            "FName": "Jane",

             "LName": "Doe"    }

    ]

};

     

// Create JSON

lco_JSM.setJSON({'lcs_Data'});

// Bind Model To Core

sap.ui.getCore().setModel(lco_JSM);

I get a syntax error. Alternatively when I load the JSON file the same set of data works, But right now, I'm getting a syntax error when I use JSON data residing in a variable.

Thanks in advance.

Accepted Solutions (1)

Accepted Solutions (1)

pfefferf
Active Contributor
0 Kudos

Hi,

why are you passing in lco_JSM.setJSON({'lcs_Data'}); the Ics_Data variable in JSON format again. The variable contains the JSON.

Change your coding to Ico_JSM.setJSON(Ics_Data);

Best Regards,

Florian

0 Kudos

Actually I was playing around and "lco_JSM.setJSON(lcs_Data);" was not working hence I added the the quotes. But when I use "lco_JSM.setJSON(lcs_Data);", it gives me a syntax error during debugging. There are plenty of source online that use this method, but I still don't get why I'm getting a syntax error. PS: I didn't realize I pasted the wrong section!

0 Kudos

This is the actual code that doesn't work:


// Sample JSON Data

var lcs_Data =

{    "Data":

    [    {    "Title" : "Mr.",

            "FName" : "John",

            "LName" : "DOe" },

         {    "Title": "Mrs.",

            "FName": "Jane",

             "LName": "Doe"    }

    ]

};

       

// Create JOSN Model Instance

var lco_JSM = new sap.ui.model.json.JSONModel();

// Create JSON

lco_JSM.setJSON(lcs_Data);

pfefferf
Active Contributor
0 Kudos

Right, you can use the "setData" method to set your JSON object. If you wanna use "setJSON" you have to convert back your JSON object to a string.

Option 1: Ico_JSM.setData(Ics_Data);

Option 2: Ico_JSM.setJSON(JSON.stringify(Ics_Data));

0 Kudos

Thanks. I was really scratching my head on that one!

Answers (2)

Answers (2)

saivellanki
Active Contributor
0 Kudos

Hi Jibran,

It should be lco_JSM.setData(JSON.stringify(lcs_Data)) , try this -


// Create JOSN Model Instance

var lco_JSM = new sap.ui.model.json.JSONModel();

// Sample JSON Data

var lcs_Data =

{    "Data":

    [    {    "Title" : "Mr.",

            "FName" : "John",

            "LName" : "DOe" },

         {    "Title": "Mrs.",

            "FName": "Jane",

             "LName": "Doe"    }

    ]

};   

// Create JSON

lco_JSM.setData(JSON.stringify(lcs_Data));

// Bind Model To Core

sap.ui.getCore().setModel(lco_JSM);

Since, setJSON method expects string data.

API - JsDoc Report - SAP UI development Toolkit for HTML5 - API Reference - sap.ui.model.json.JSONModel

Regards,

Sai Vellanki.

Former Member
0 Kudos

hi,

Try this

var oModel = new sap.ui.model.json.JSONModel();

      oModel.setData({

        modelData: arraydata

      });

sap.ui.getCore().setModel(oModel);

0 Kudos

As already mentioned in my post, this method works. But when I declare it in a variable, it doesn't work.