1

I am new to JavaScript. I have done similar requirements in Java using org.json library.

I have a String:

var string = "{\"id\" :[\"\"],\"State\" :[\"TX\",\"IA\"]}";

I am converting that string to a JSONObject using this:

var obj = JSON.parse(string);

I am trying to achieve this using JavaScript or jQuery. How do i get the JSONArrays inside this JSONObject. Please help me. Thanks in advance.

J3STER
1,0752 gold badges13 silver badges29 bronze badges
asked Mar 16, 2017 at 4:44
9
  • 1
    Hi Rayon. But i am trying to achieve this using javascript only. Do you still think i need to add java tag? Please let me know i will add if it helps. Commented Mar 16, 2017 at 4:52
  • I read it wrong. You have already achieved it using JSON.parse, what is left to be achieved ? Commented Mar 16, 2017 at 4:54
  • 3
    you just do obj.id and you've got the array. Or obj.id.length for the length of it (should be 1 here) Commented Mar 16, 2017 at 4:55
  • @Rayon I am trying to access the JSONArrays inside them. Commented Mar 16, 2017 at 4:56
  • 1
    @user7637864 that's because your id array contains one item; an empty string Commented Mar 16, 2017 at 5:17

3 Answers 3

2
var jsonStr = "{\"id\" :[\"\"],\"State\" :[\"TX\",\"IA\"]}";
var jsonObj = JSON.parse(jsonStr);
idArr=jsonObj.id;
stateArr=jsonObj.State;
idArr.forEach(function(id) {
 console.log(id);
});
stateArr.forEach(function(state) {
 console.log(state);
})

hope this will help you

answered Mar 16, 2017 at 5:11
Sign up to request clarification or add additional context in comments.

5 Comments

but the idArr.length is giving me 1. That should not be the case right?
idArr is a String array and it contains one empty String in its 0th index, That is why idArr.length is 1
Can you please let me know how to get rid of that Empty String. I have tried (obj.id.isEmpty()) check . But its not working. @Jackson Baby
1

You can try this with jquery Mention jquery as :

<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Code or script

var obj = $.parseJSON( '{\"id\" :[\"dddd\"],\"State\" :[\"TX\",\"IA\"]}' );
console.log(obj.id);
Isaac
11.8k5 gold badges36 silver badges45 bronze badges
answered Mar 16, 2017 at 5:12

1 Comment

Thanks for your response @arjun.
0

So once you have got the object, you have to get the array field on which you want to perform iteration. For Ex if you want to iterate over State, Check below:


for (var i=0;i<obj.State.length;i++)
 {
 obj.State[i] ;
 }

Is this what you were looking for? If not, then pls be more clear.

answered Mar 16, 2017 at 5:05

Comments

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.