-3

I have a JSON code coming from AjaxRequest and I want it to split the object into a string or array in order for me to manipulate the data. Here's my JSON Code

[{
    "intAccountId": 2733,
    "strAccountId": "59250-2001",
    "strDescription": "SOIL TEST & GPS SERVICE-WB",
    "strNote": null,
    "intAccountGroupId": 6,
    "dblOpeningBalance": null,
    "ysnIsUsed": false,
    "intConcurrencyId": 1,
    "intAccountUnitId": null,
    "strComments": null,
    "ysnActive": true,
    "ysnSystem": false,
    "strCashFlow": null,
    "intAccountCategoryId": 47
}]

The result will something like this.

"2733 59250-2001 SOIL TEST & GPS SERVICE-WB"
Lex Eugene
  • 15
  • 7
  • 2
    Using which language? What have you tried so far? – Gergo Erdosi Apr 16 '15 at 06:44
  • Check those out: http://stackoverflow.com/questions/3593046/jquery-json-to-string http://stackoverflow.com/questions/45015/safely-turning-a-json-string-into-an-object – Dashovsky Apr 16 '15 at 06:46
  • Also, isn't the whole point of getting JSON across that you can simply access the elements of the resulting object using things like `jsonobject["strAccountId"]`? Building a string out of that sounds like it should be covered by your most basic understanding of whatever language (probably Javascript? If so, please add that tag to your question) you're using. – Marcus Müller Apr 16 '15 at 06:47
  • Im using Sencha EXT JS. – Lex Eugene Apr 16 '15 at 07:04

1 Answers1

0

From the looks of it, I don't think you want to use JSON.stringify().

I am not sure of your exact output requirements but you could have searched well before asking. Anyways, Assuming you want to do that in JavaScript, here's what you can do.

Let theResponse be the variable representing your array.

1. You need a string made of just the first 3 keys?

var requiredString = [theResponse[0].intAccountId, 
                       theResponse[0].strAccountId,
                       theResponse[0].strDescription
                      ].join(" ");

2. You need a string made of all keys?

var requiredString = [];
for(var key in theResponse[0]){
    requiredString.push(theResponse[0][key]); //obviously there are better ways.
}
requiredString = requiredString.join(" ");

How will you handle the null values is upto you. May be within the loop you can check if theResponse[0][key] is null and if true, push a string like "NA" instead.

EDIT - Retaining the Indicators As you asked, Use JSON.stringify to convert your object to string containing all keys and values. A little bit of post processing may even give you better structure.

EXAMPLE

var theOtherString = JSON.stringify(theResponse[0]);
console.log(theOtherString); // your JSON string.
console.log(theOtherString.replace(/"/g,"").replace(/,/g, " ")); //post processed a little.

This can go on forever based on what you need.

Praveen Puglia
  • 5,577
  • 5
  • 34
  • 68