Skip to Content

Help with JSON Model

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.

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

3 Answers

  • Best Answer
    Posted on Oct 13, 2015 at 11:11 AM

    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

    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on Oct 13, 2015 at 11:10 AM

    hi,

    Try this

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

    oModel.setData({

    modelData: arraydata

    });

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

    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on Oct 13, 2015 at 11:35 AM

    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.

    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.