8

Dynamically I am getting an array.

For example we can consider this following array.

var sampleArray=[
        "logo",
        "Details",
        "titles"
    ];

But I want it something like this.

jsonObj={
"poistion1":"logo",
"poistion2":"Details",
"poistion3":"titles"
}
1
  • var sampleArray=[ "logo", "Details", "titles" ]; String[] stringArray = Arrays.copyOf(sampleArray, sampleArray.length, String[].class); JSONArray mJSONArray = new JSONArray(Arrays.asList(stringArray)); for(int n = 0; n < mJSONArray.length(); n++) { JSONObject object = mJSONArray.getJSONObject(n); // do some stuff.... } Commented Jul 14, 2015 at 6:28

6 Answers 6

4

You can iterate on array and create object like following

var jsonObj = {};
for (var i = 0 ; i < sampleArray.length; i++) {
    jsonObj["position" + (i+1)] = sampleArray[i];
}
Sign up to request clarification or add additional context in comments.

2 Comments

How about if you want all the keys to only read position?
@ZeroDarkThirty - Not clear. Can you please elaborate?
4

Like this

var jsonObj = {};

var sampleArray = [
    "logo",
    "Details",
    "titles"
];

for (var i = 0, len = sampleArray.length; i < len; i++) {
    jsonObj['position' + (i + 1)] = sampleArray[i];
}

console.log(jsonObj);

Comments

3

You can create an empty object, then loop over(Array.forEach()) the array and assign the value

var sampleArray = [
  "logo",
  "Details",
  "titles"
];
var obj = {};
sampleArray.forEach(function(value, idx) {
  obj['position' + (idx + 1)] = value
});

snippet.log(JSON.stringify(obj))
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>
<!-- Provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->

5 Comments

forEach will not be supported in ie lower versions
@raghavendra there is polyfill for that purpose
asking person don't even know basics about loops. better we can teach them basics loops which will support in standards.
would you pls explain about polyfill. does it support in ie6?
It's 2015, I thought by now we would be beyond asking about support for the officially dead IE6...around only 1.7% of the world uses a browser (old IE) that does not support forEach (ECMAScript 5)
2
var arr=[
        "logo",
        "Details",
        "titles"
    ];
var result = {};
  for (var i = 0; i < arr.length; ++i){
      result["position" + (i+1)] = arr[i];
 }

Comments

1

You can use the JSON Object:

var yourObject = [123, "Hello World", {name: "Frankie", age: 15}];
var yourString = JSON.stringify(yourObject); // "[123,"Hello World",{"name":"Frankie","age":15}]"

the JSON object has also JSON-to-Object functionality:

var anotherObject = JSON.parse(yourString);

1 Comment

the input is in array, and the required output is in json format with ` jsonObj={ "poistion1":"logo", "poistion2":"Details", "poistion3":"titles" } ` Is your output working and showing the correct output? I dont think so
0

try this

var obj = {};
var sampleArray=[
        "logo",
        "Details",
        "titles"
    ];

for(var index in  sampleArray) {
     obj['pos' + index] = sampleArray[index];
   }

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.