javascript - Remove String from JSON - Stack Overflow

I want to feed a dijit.form.Select (Dojo Select-Box) with a Data-Store (JsonRest). The Problem is, that

I want to feed a dijit.form.Select (Dojo Select-Box) with a Data-Store (JsonRest). The Problem is, that the JSON-Parser only accepts JSON in this format:

[
  {id:"1", name:"One1"},
  {id:"2", name:"Two1"}
];

The REST-API of the webapplication we want to call delivers the following JSON:

{
  "data": [
      {id:"1", name:"One1"},
      {id:"2", name:"Two1"}
],
    "total": 2,
    "start": 0,
    "sort": "name",
    "order": "asc",
     "size": 2
};

That is why the Select Box does not show any data. Therefore, we need to remove the {"data": part and the last part of the JSON message bevore passing it to the Dojo Select Box.

The Data is stored in a JsonRest Object. So the question is how we can remove the first and the last part of the JSON in a way that simply this here is given to the Select-Box:

[
  {id:"1", name:"One1"},
  {id:"2", name:"Two1"}
];

Thank you for your answers and best regards Ben

I want to feed a dijit.form.Select (Dojo Select-Box) with a Data-Store (JsonRest). The Problem is, that the JSON-Parser only accepts JSON in this format:

[
  {id:"1", name:"One1"},
  {id:"2", name:"Two1"}
];

The REST-API of the webapplication we want to call delivers the following JSON:

{
  "data": [
      {id:"1", name:"One1"},
      {id:"2", name:"Two1"}
],
    "total": 2,
    "start": 0,
    "sort": "name",
    "order": "asc",
     "size": 2
};

That is why the Select Box does not show any data. Therefore, we need to remove the {"data": part and the last part of the JSON message bevore passing it to the Dojo Select Box.

The Data is stored in a JsonRest Object. So the question is how we can remove the first and the last part of the JSON in a way that simply this here is given to the Select-Box:

[
  {id:"1", name:"One1"},
  {id:"2", name:"Two1"}
];

Thank you for your answers and best regards Ben

Share Improve this question edited Mar 13, 2014 at 12:10 Ben asked Mar 13, 2014 at 11:34 BenBen 1,5994 gold badges20 silver badges34 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 3

Create a new Array variable from the data for the dojo select seems simplest ...

var restapidataObj = {
  "data": [
      {id:"1", name:"One1"},
      {id:"2", name:"Two1"}
    ],
    "total": 2,
    "start": 0,
    "sort": "name",
    "order": "asc",
     "size": 2
};

var dojoSelectArray = restapidataObj.data;

Now pass dojoSelectArray to dojo

Store the result in a object and access the data property of that object.

var result = {
  "data": [
  {id:"1", name:"One1"},
  {id:"2", name:"Two1"}
],
   "total": 2,
   "start": 0,
   "sort": "name",
   "order": "asc",
   "size": 2
};

result.data would give you the data array.

Check the javascript object documentation at MDN

resultFromServer =

     {
       "data": [
           {id:"1", name:"One1"},
           {id:"2", name:"Two1"}
     ],
         "total": 2,
         "start": 0,
         "sort": "name",
         "order": "asc",
          "size": 2
     };

this convert to object (Use this incase resultFromServer is a string)

    var Output = eval('('+ resultFromServer+')')

this gives your output

    JSON.stringify(Output.data)

gives the string

    "[{"id":"1","name":"One1"},{"id":"2","name":"Two1"}]"

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745265863a4619452.html

相关推荐

  • javascript - Remove String from JSON - Stack Overflow

    I want to feed a dijit.form.Select (Dojo Select-Box) with a Data-Store (JsonRest). The Problem is, that

    3小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信