1

My Java code returns a Collection (ArrayList) and the resulting JSON produced by JAXB looks like:

{"todo":[{"name":"CAMPBELL","sales":"3","time":"1331662363931"},
{"name":"FRESNO","sales":"2","time":"1331662363931"}]}

But, is there a way I can make it look like:

[{"name":"CAMPBELL","sales":"3","time":"1331662363931"},
{"name":"FRESNO","sales":"2","time":"1331662363931"}]

Is there a way in Java/JAXB or maybe in AJAX callback using responseText. .

BTW, I've also tried with Java array but it made no difference.

Any help will be appreciated.

0

2 Answers 2

1

All you need is:

var todo = resp.todo;

where resp is the whole JSON response. Note that this is actually good design. A root array is not recommended due to JSON hijacking.

Sign up to request clarification or add additional context in comments.

3 Comments

Did you mean responseText.todo? I tried both resp.todo and responseText.todo in FireBug I can see the value of var todo as undefined
@user655577, Felix is right. You do need to parse the JSON. Are you using a library or doing it yourself? If the latter, you can use json2.
Sorry my bad, I forgot to parse the JSON. It's working now. It was simple, but i'm new to JavaScript.
0

Note: I'm the EclipseLink JAXB (MOXy) lead and a member of the JAXB 2 (JSR-222) expert group.

Instead of correcting bad JSON on the client, you could fix the problem on the server. Below is an example of how EclipseLink JAXB (MOXy) could be used to produce the desired JSON:

Demo

package forum9689970;

import java.io.StringReader;
import java.util.List;
import javax.xml.bind.*;
import javax.xml.transform.stream.StreamSource;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(SalesPerson.class);

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        unmarshaller.setProperty("eclipselink.media-type", "application/json");
        unmarshaller.setProperty("eclipselink.json.include-root", false);
        String jsonString = "[{\"name\":\"CAMPBELL\",\"sales\":\"3\",\"time\":\"1331662363931\"},{\"name\":\"FRESNO\",\"sales\":\"2\",\"time\":\"1331662363931\"}]";
        StreamSource json = new StreamSource(new StringReader(jsonString));
        List<SalesPerson> salesPeople = (List<SalesPerson>) unmarshaller.unmarshal(json, SalesPerson.class).getValue();

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.setProperty("eclipselink.media-type", "application/json");
        marshaller.setProperty("eclipselink.json.include-root", false);
        marshaller.marshal(salesPeople, System.out);
    }

}

SalesPerson

package forum9689970;

import javax.xml.bind.annotation.*;

@XmlAccessorType(XmlAccessType.FIELD)
public class SalesPerson {

    private String name;
    private String sales;
    private String time;

}

Output

[ {
   "name" : "CAMPBELL",
   "sales" : "3",
   "time" : "1331662363931"
}, {
   "name" : "FRESNO",
   "sales" : "2",
   "time" : "1331662363931"
} ]

For More Information

Comments

Your Answer

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.