Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7030577
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:41:21+00:00 2026-05-28T00:41:21+00:00

I have tried to write an utility function that can convert my custom data

  • 0

I have tried to write an utility function that can convert my custom data (data Sencha Touch stores use) to JSON format and I am almost done but it fails with complex data coming from twitter API but works fine with simple type of data.

Custom Data

var items = [];
for (var i = 0; i < 10; i++) {
   var item = {};

   var data = {};
   data.prop1 = "123456789";
   data.prop2 = "Some Name";
   data.prop3 = "Some Date and Time";

   item.data = data;

   items.push(item);
}

Now above data can be accessed in a loop and can be converted to JSON with following function.

function toJSON(items) {
    var jsonString = "[";

    for (var i = 0; i < items.length; i++) {
        var item = items[i];

        jsonString += "{";

        for (var propertyName in item.data) {
            jsonString += '"' + propertyName + '":' + '"' + item.data[propertyName] + '",';
        }

        if (jsonString.substr(jsonString.length - 1, 1) === ",") {
            jsonString = jsonString.substr(0, jsonString.length - 1);
        }

        jsonString += "},";
    }

    if (jsonString.substr(jsonString.length - 1, 1) === ",") {
        jsonString = jsonString.substr(0, jsonString.length - 1);
    }

    jsonString += "]";

    return jsonString;
}

The question is if I am doing the encoding thing right?

You can see these fiddle to get real time experience http://jsfiddle.net/WUMTF/ and http://senchafiddle.com/#gxtZ9

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T00:41:22+00:00Added an answer on May 28, 2026 at 12:41 am

    Is there any reason why you don’t just use the native JSON.stringify() method, or the (wrapped) versions provided by your favorite JavaScript / AJAX library? They will be more robust, tested, secure, and better performing. (Most of the library versions will simply call the browser’s native methods if available.)

    Your toJSON implementation may be working for your simple test data, but is failing for more complex types of data for many reasons, but most probably most significantly, because it fails to account for any type of nesting. It assumes that the top level is always an array, and that each element in the array only has properties one level deep. Take a look at https://github.com/douglascrockford/JSON-js/blob/master/json.js . This is the implementation provided by Douglas Crockford, the “father” of JSON. Pretty much everything shown there is required for a valid implementation. Unfortunately, the odds are against you if you think that you can easily and simply recreate this in a short amount of time (short of copy and paste).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am a newbie. I have tried to write a template function that gets
I have tried to write an Android application with an activity that should be
I have tried to write: Event EventName(Of T)() But Visual Studio tells me that
I have tried to use ASP.NET MVC for a while, then I face a
I have tried to write a cookies file to store userID and userPw by
I have tried to write the console output to a txt file using this
I tried to write a Utility Manager application, and I am trying to test
I have a custom asp.net mvc class validation attribute. My question is how can
I have a query that simply fetch data from my database, this query returns
I am just a beginner in php. I have tried to write code for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.