简体   繁体   中英

How to change a javascript array to a JSON object for angular-schema-form?

I need some help please converting a javascript generated array that I am preparing to be used by the angular-schema-form (requires a JSON Object).

I create an array using a for loop with some field items that I have retrieved from a $http request.

I am able to create the array without issue however, when I now try to prepare it for the angular-schema-form object this is where I am having issues converting it to the required JSON object format.

I create the array in javascript like this,

var objectSchema = [];

for(index = 0; index < instanceFields.length; index++)
{
    var fieldObject = [];

    fieldObject.id = instanceFields[index].id;
    fieldObject.title = instanceFields[index].description;
    fieldObject.type = pass_type;
    fieldObject.value = instanceFields[index].value.raw;

    objectSchema.push(fieldObject);

}

This is a console.log of the array.

console.log(objectSchema);

// result

0: Array[0]
   id: "103859"
   length: 0
   title: "Summary"
   type: "string"
   value: ""
1: Array[0]
   id: "101842"
   length: 0
   title: "Job Type"
   type: "string"
   value: "696"

I have tried to convert the above to JSON using JSON.stringify but this is returning an empty result.

I have also tried to pass the array to angula.toJson but I am getting an empty results as well.

var myJsonString = angular.toJson(objectSchema, true);
console.log(myJsonString);

// result 

[
  [],
  []
]

If you have any direction for me please to follow it would be greatly appreciated.

Thanks B

You are seeing this behavior because you are instantiating fieldObject as an array rather than as an object. You then try to add properties to this array, which doesn't really make sense and more importantly doesn't actually fill the data structure as you are intending.

What then happens when you try to serialize, is that the array (which is still empty), serializes as an empty array and you lose the object properties on it.

Try:

var fieldObject = {};

Here is a fiddle demonstrating the behavior - http://jsfiddle.net/ko8d0w5b/

Try this fiddle.. http://jsfiddle.net/lalith_ram/809ubtvj/ Instantiate the variable fieldObject as an object.

var objectSchema = {};

for(index = 0; index < 1; index++){

    var fieldObject = {};
    fieldObject.id = "101";
    fieldObject.title = "Hello";
    fieldObject.type = "sometype";
    fieldObject.value = "somevalue";
    objectSchema = fieldObject;


}

var test = JSON.stringify(objectSchema);

alert(test);

I use Microsoft's JavaScriptSerializer and it works fine for us. AFAIK, the JavaScript format is the same thing as JSON format (that's the "JS" in "JSON"). Use it like this:

JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();
string myJsonString;

myJsonString = javaScriptSerializer.Serialize(myObject);

There may be other serializers that are faster or better, but this is a quick way to do it using just Microsoft's tools.

And here's a reference to Microsoft's DataContractJsonSerializer, which looks more complicated: https://msdn.microsoft.com/en-us/library/bb412179(v=vs.110).aspx

Does this help?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM