Luffy
Luffy

Reputation: 1335

Trying to get data from web-service in java

[
    {
        "title": "ginger",
        "id": "38",
        "product_id": "17",
        "product_logo": "imagePath/Desert_0.jpg?itok=Uvm6nxpY",
        "product_logo_1": "imagePath/Desert_0.jpg?itok=6YHK_afq",
        "price": "$12.00"
    }
]

I'm working on a swing application in Java in which I need to fetch data from web-service

I'm following this example to get data from web service and it store web service data in String.

But I want to convert this string to json and follow some example one of them is this and this

so I just want to know what I'm doing wrong here, or this is the right way to use this, because I don't know how to use web services in Java...

Upvotes: 3

Views: 4409

Answers (3)

Zoro
Zoro

Reputation: 120

You need to create its object first and then you need to use this

JSONObject jsnobject = new JSONObject(String);

JSONArray jsonArray = jsnobject.getJSONArray("locations");
for (int i = 0; i < jsonArray.length(); i++) {
    JSONObject explrObject = jsonArray.getJSONObject(i);
}

and make sure you import this one in your code org.json.JSONObject

Upvotes: 4

Imran khan
Imran khan

Reputation: 847

You can use google gson jar.

Json Jar link

and convert the object to json as below. EmployeeRestService.java

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.web.object.Employee;

@Path("/getemployee")
public class EmployeeRestService {
    @GET
    @Path("/emplist")
    @Produces(MediaType.APPLICATION_JSON)
    public Object getEmpList() throws SQLException{

        String query = "SELECT ID,NAME,SURNAME,LOCATION_ID FROM FW_EMPLOYEE ORDER BY ID";
        Connection dbcon = (new DbConnection()).getDBConnection();
        Statement stmt = dbcon.createStatement();
        ResultSet rs = stmt.executeQuery(query);
        List<Employee> empList = new ArrayList<Employee>();
        while(rs.next()){
            Employee emp = new Employee();

            emp.setId(rs.getString(1));
            emp.setFirstName(rs.getString(2));
            emp.setLastName(rs.getString(3));
            emp.setLocationId(rs.getString(3));

            empList.add(emp);
        }
         dbcon.close();
         Gson gson = new GsonBuilder().create();

        return gson.toJson(empList);
    }
}

Json call using ajax.

 <script type="text/javascript">
$(document).ready(function(){
    var val = "";
    var row ="";
    $("#submit").click(function(event){
        event.preventDefault();

        $.ajax({
            type: "GET",
            dataType:"json",
            //contentType: "application/json; charset=utf-8",
            url:  "rest/getemployee/emplist",
            success: function(data) {
                console.log("response:" + data);
                $.each(data, function(j, pdata) {
                    row = '<tr> <td>'+ pdata.Id +'</td><td>'+  pdata.firstName +'</td> <td>' + pdata.lastName + '</td></tr>';
                    val = val + row;
                    //$('#data').text(row);
                  // console.log(pdata.lastName);
                 });

               $("#emp").append( '<table border="1">'+ val + '</table>');
            },
            error: function(jqXHR, textStatus, errorThrown) {
                console.log(' Error in processing! '+textStatus);

            }
        });
     });

});
</script>

Upvotes: 1

hii
hii

Reputation: 255

You can get the JSONObject as follow

    JSONObject jsonObj = new JSONObject(YOUR_STRING_OBJECT);

Check This for more

Upvotes: 2

Related Questions