1

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

3
  • i see no problems in stringify and parse, jsfiddle.net/tt1teL0b/1 Commented Feb 16, 2015 at 20:21
  • Hi, thanks for the comment, I am just confused as to why my JSON array is empty, the amount of elements is correct but nothing else. Commented Feb 16, 2015 at 20:54
  • well it's weird, don't know but i see you fixed finally so glad for you ;) Commented Feb 16, 2015 at 22:07

3 Answers 3

5

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/

Sign up to request clarification or add additional context in comments.

1 Comment

@BrentR Just keep in mind that javascript does not have any "out of the box" concept of an associative array or hash table, which is what it seems you were originally trying achieve.
1

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);

1 Comment

I what was does this answer provide any different information than the one already accepted?
-1

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?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.