2

I have JSONArray as below :

[["title","details"],["abc","xyz"],["abc2","xyz2"]]

How to transform into Array of JSONObject as below using Java /JavaScript?

[ { 'title': abc, 'details':xyz,}, {'title': abc2, 'details':xyz2}]
asked Jun 6, 2012 at 10:07
3
  • Is it really java you want to use??? Commented Jun 6, 2012 at 10:14
  • Yes using existing Java library form org.json or jQuery. Commented Jun 6, 2012 at 10:17
  • 1
    jQuery is no java library!!!1!11 Commented Jun 6, 2012 at 10:34

4 Answers 4

1

Below code was modified from the solution here to allow multiple arrays of values to be passed.

function makeObject(keys, array) {
 var output = [];
 for (var o = 0; o < array.length; o++) {
 var object = {};
 for (var i = 0; i < array[o].length; i ++ ) {
 object[keys[i]] = array[o][i];
 }
 output.push(object);
 }
 return output;
}
// input array
var array = [["title","details"],["abc","xyz"],["abc2","xyz2"]]; 
// extract keys leaving only values in array
var keys = array.shift();
// build object
var output = makeObject(keys, array);

Example: http://jsfiddle.net/u54tT/1/

answered Jun 6, 2012 at 20:37
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks man, very helpful answer. I was looking around for JQuery implementation and didn't notice that JS still able to transform into json.
1

I have found a workaround for this for now. Still looking for build in function or concrete implementation on this.

String inputStr = "[[\"title\",\"details\"],[\"abc\",\"xyz\"],[\"abc2\",\"xyz2\"]]";
try {
 JSONArray inputArray = new JSONArray(inputStr);
 JSONArray outputArray = new JSONArray();
 for (int i = 0; i < inputArray.length(); i++) {
 JSONArray inArr = inputArray.getJSONArray(i);
 for (int j = 0; j < inArr.length(); j++) {
 if (i != 0) {
 outputArray.put(new JSONObject().put(
 inputArray.getJSONArray(0).getString(j), inArr.get(j)));
 }
 }
 }
 System.out.println("outputArray = " + outputArray.toString());
} catch (JSONException jse) {
 System.out.println("jse = " + jse.toString());
}

Output:

outputArray = [{"title":"abc"},{"details":"xyz"},{"title":"abc2"},{"details":"xyz2"}]
answered Jun 6, 2012 at 11:15

Comments

0

If I understood the task correctly there no method that will this job since it seems to be a bit non-trivial case. So you should write your own transformation..

answered Jun 6, 2012 at 10:42

Comments

0

Is this what you are looking for ?

jQuery.parseJSON( json ) Returns: Object

Description: Takes a well-formed JSON string and returns the resulting JavaScript object. http://api.jquery.com/jQuery.parseJSON/

DaveShaw
52.9k18 gold badges118 silver badges142 bronze badges
answered Jun 6, 2012 at 10:39

1 Comment

I'm looking to parse array. Please refer to question above.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.