3
[
    {
        "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...

Community
  • 1
  • 1
Luffy
  • 1,317
  • 1
  • 19
  • 41

3 Answers3

4

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

Luffy
  • 1,317
  • 1
  • 19
  • 41
Zoro
  • 120
  • 9
2

You can get the JSONObject as follow

    JSONObject jsonObj = new JSONObject(YOUR_STRING_OBJECT);

Check This for more

hii
  • 255
  • 4
  • 18
  • I already use this one I have string not String object.. to use this I need to pass String object – Luffy Apr 15 '15 at 07:07
  • only problem is that I'm not importing correct file I need to Import "import org.json.JSONObject;" this ... sorry and Thanks :) – Luffy Apr 15 '15 at 07:31
1

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>
Imran khan
  • 819
  • 3
  • 12
  • 24
  • Actually I'm Working on a swing application – Luffy Apr 15 '15 at 07:10
  • 1
    @Luffy : try to use this link , may be it will help for you .https://icewalker2g.wordpress.com/2010/05/17/using-json-in-java-swing/, Other simple way for json parsing :http://javac.in/?p=220 – Imran khan Apr 15 '15 at 07:17
  • Thanks Imran. I'll check your suggested link look like this is what I'm looking for... Thanks :) – Luffy Apr 15 '15 at 07:20