0

java webservices returning this value

{"employee":[{"address":"New Delhi","employeeId":"22222","employeeName":"Abhishek","jobType":"Marketing","salary":"50000"},{"address":"Noida","employeeId":"11111","employeeName":"Dineh Rajput","jobType":"Sr.Software Engineer","salary":"70000"}]}

but I want only

[{"address":"New Delhi","employeeId":"22222","employeeName":"Abhishek","jobType":"Marketing","salary":"50000"},{"address":"Noida","employeeId":"11111","employeeName":"Dineh Rajput","jobType":"Sr.Software Engineer","salary":"70000"}]

my java webservices main code this:

 @GET
     @Path("/json/employees/")
     @Produces("application/json")
     public List<Employee> listEmployeesJSON(){
         return new ArrayList<Employee>(employees.values());
     }
Mel
  • 5,837
  • 10
  • 37
  • 42
Naveen Maurya
  • 197
  • 1
  • 1
  • 10

3 Answers3

0

The value you are expecting is not valid due to W3 JSON definition. You can use returned value same as you would use the expected one anyway.

Finally remember that JSON itself is nothing more that string so you can operate on it as on string for example:

import java.util.regex.*;

public class ExpectedJSON{

     public static void main(String []args){
        String string = "{\"employee\":[{\"address\":\"New Delhi\",\"employeeId\":\"22222\",\"employeeName\":\"Abhishek\",\"jobType\":\"Marketing\",\"salary\":\"50000\"},{\"address\":\"Noida\",\"employeeId\":\"11111\",\"employeeName\":\"Dineh Rajput\",\"jobType\":\"Sr.Software Engineer\",\"salary\":\"70000\"}]}";
        String regex = "^[^\\[]+(.+)\\}$";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(string);

        matcher.find();
        String group = matcher.group(1);

        System.out.println(group);
     }
}
m.antkowicz
  • 13,268
  • 18
  • 37
0

How are you constructing the json object? If you want your json to not contain the employee key, then just remove it from where you are adding it. Both json formats are valid, you can check your json structure here.

Luís Cunha
  • 154
  • 2
  • 11
  • Cunha how i can remove employee key – Naveen Maurya Jan 06 '16 at 11:26
  • Cunha > am constructing json object like this: – Naveen Maurya Jan 06 '16 at 11:30
  • /* private static Map employees = new HashMap(); private static List list=new ArrayList(); static { Employee employee1 = new Employee(); employee1.setEmployeeId("11111"); employee1.setEmployeeName("Dineh Rajput"); employee1.setJobType("Sr.Software Engineer"); employee1.setSalary(70000l); employee1.setAddress("Noida"); list.add(employee1); employees.put(employee1.getEmployeeId(),employee1) } – Naveen Maurya Jan 06 '16 at 11:31
0

I Wonder why do you need to remove the key of JsonArray Object, if you remove it you lost the reference to the values. By the way,if you think that this way is your solution you can use the gson library.

You can find the solution here.

Hope this help.

Community
  • 1
  • 1
ImLearning
  • 200
  • 1
  • 12