rajan416
asked on
How to get convert json array to comma separated values
I would like to convert the following json array to comma separated values
i/p:
[{"filter":"ID","filterVal ues":["050 886","0508 85","05088 4","050883 ","050882" ]}]
expected o/p;
String field="ID"
String values= "050886","050885","050884" ,"050883", "050882";
I tried the below code , but I am getting exception while getting filter values. Any help would be appreciated.
i/p:
[{"filter":"ID","filterVal
expected o/p;
String field="ID"
String values= "050886","050885","050884"
I tried the below code , but I am getting exception while getting filter values. Any help would be appreciated.
String input ='[{"filter":"ID","filterValues":["050886","050885","050884","050883","050882"]}]';
JSONArray jsonarray = (JSONArray) new JSONTokener(input ).nextValue();
for (int i = 0; i < jsonarray.length(); i++) {
JSONObject jsonobject = jsonarray.getJSONObject(i);
String field= jsonobject.getString("filter");
String values= jsonobject.getString("filterValues");
}
ASKER
Hi,
The getJSONObject throws compile time error for converting to string...
I tried jsonobject.getJSONArray("f ilterValue s").toStri ng(); and got ["050886","050885","050884 ","050883" ,"050882"] . How can i convert this to below format
"050886,050885,050884,0508 83,050882"
The getJSONObject throws compile time error for converting to string...
I tried jsonobject.getJSONArray("f
"050886,050885,050884,0508
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
This is not working....
I'm getting the following output:
050886,050885,050884,05088 3,050882
what are you getting?
050886,050885,050884,05088
what are you getting?
ASKER
This is perfect...Thank you guys...
Although, it won't give you the quotes around each of the filter values.
Guyz, Dont struggle that much ,,,here is the best way I have:
var distStateIds = [];
var stateIds = distStateIds.map(function (val) {
return val;
}).join(',');
var distStateIds = [];
var stateIds = distStateIds.map(function (val) {
return val;
}).join(',');
What exception do you get and what JSONTokener are you using ?
http://www.massapi.com/class/js/JSONTokener-1.html#Example6
And I believe this should work
Open in new window
Regards,
Tomas Helgi