-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"
4
  • 2
    Using which language? What have you tried so far? Commented Apr 16, 2015 at 6:44
  • Check those out: stackoverflow.com/questions/3593046/jquery-json-to-string stackoverflow.com/questions/45015/… Commented Apr 16, 2015 at 6: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. Commented Apr 16, 2015 at 6:47
  • Im using Sencha EXT JS. Commented Apr 16, 2015 at 7:04

1 Answer 1

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.

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

6 Comments

I will definitely try that Sir.
Got it Sir, the Problem is my JSON is dynamic data so the requiredString [theResponse[0].intAccountId] is not applicable approach?
So I assume the second one worked!
The Warlock please edit your post and change JSON.stringity to JSON.stringify
@Warlock, What if I want to retain the Obeject name like this result for indicator purpose only. intAccountId: 2733 strAccountId: 59250-2001 strDescription: SOIL TEST & GPS
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.