0

I am currently experiencing this weird issue.

I have a valid JSON object just like this:

var myjson = '[{"text": {"tag": "question","content": "question content 1"}}, {"text": {"tag": "answer","content": "answer content1"},"text": {"tag": "question","content": "question content 2"},"text": {"tag": "answer","content": "answer content2"}}]';

Then I proceed to transform it into a normal javascript array of objects like so:

var parsed = JSON.parse(myjson);
console.log(parsed);

For some weird reason the parsed Array only contains the first and last object ....

I have prepared a fiddle for this here

noa-dev
  • 3,561
  • 9
  • 34
  • 72
  • 1
    Because your second object has the same key three times, it's not actually four objects in an array. – jonrsharpe Jun 25 '16 at 17:39
  • does this mean I have to change it to [{"text1": {....}}, {"text2": {....}}, ... ] ? – noa-dev Jun 25 '16 at 17:41
  • Nno, you only need one fix or the other; that one has both, which is redundant. – jonrsharpe Jun 25 '16 at 17:44
  • You are missing a couple brackets if you intended to have an array with a bunch of objects. No problem with the objects having the same keys as long as they are seperate objects - this should work: `var myjson = '[{"text": {"tag": "question","content": "question content 1"}}, {"text": {"tag": "answer","content": "answer content1"}}, {"text": {"tag": "question","content": "question content 2"}},{"text": {"tag": "answer","content": "answer content2"}}]';` – IrkenInvader Jun 25 '16 at 17:46

4 Answers4

2

You use the same key multiple times, the last one will overwrite all previous ones. Use unique keys or put the values of those keys in an array instead. In this post there is a link to the original spec declaring that this should be the case.

Edit: here's an example fiddle

var myjson = '[{"text": {"tag": "question","content": "question content 1"}}, {"text1": {"tag": "answer","content": "answer content1"},"text2": {"tag": "question","content": "question content 2"},"text3": {"tag": "answer","content": "answer content2"}}]';

// OR
var myjsonarray = '[{"text": {"tag": "question","content": "question content 1"}}, [{"tag": "answer","content": "answer content1"}, {"tag": "question","content": "question content 2"},{"tag": "answer","content": "answer content2"}]]'
Community
  • 1
  • 1
edwardmp
  • 6,339
  • 5
  • 50
  • 77
1

A JSON Object looks like the following

public JSONObject(Map<?, ?> map) {
    this.map = new HashMap<String, Object>();
    if (map != null) {
        for (final Entry<?, ?> e : map.entrySet()) {
            final Object value = e.getValue();
            if (value != null) {
                this.map.put(String.valueOf(e.getKey()), wrap(value));
            }
        }
    }
}

A JSON Object is basically a hashmap containing key value pair.

When we provide the same key again and again it gets overwritten. So, you are getting only the first and last value.

If you still want to have with same name and to have in a single wrapper you may try with this

[
 {"text": 
   {"tag": "question","content": "question content 1"}
 }, 
 [
   {"text": 
      {"tag": "answer","content": "answer content1"}
   }, 
   {"text": 
      {"tag": "question","content": "question content 2"}
   },
   {"text": 
      {"tag": "answer","content": "answer content2"}
   }
 ]
]

Have a look at this JSON Object java implementation to know in depth.

Tom Taylor
  • 3,344
  • 2
  • 38
  • 63
0

The JSON file may be valid, but that doesn't mean that it has the structure you intend it to have ;)

Right now, the array contains indeed two objects. In the second object you define 'text' three times, each time overwritiong the previous declaration. So you have the first text, and then the second and third get overwritten by the fourth.

JJWesterkamp
  • 7,559
  • 1
  • 22
  • 28
0

You have given each key same name that the problem. Working Fiddle

var myjson = '[{"text1": {"tag": "question","content": "question content 1"}}, {"text2": {"tag": "answer","content": "answer content1"},"text3": {"tag": "question","content": "question content 2"},"text4": {"tag": "answer","content": "answer content2"}}]';

console.log(myjson);
var parsed = JSON.parse(myjson);
console.log(parsed);
M.Tanzil
  • 1,987
  • 1
  • 12
  • 28