【问题标题】:Trying to get data from web-service in java尝试从Java中的Web服务获取数据
【发布时间】:2015-06-21 00:03:59
【问题描述】:
[
    {
        "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"
    }
]

我正在使用 Java 开发一个 Swing 应用程序,我需要在其中从 Web 服务获取数据

我正在关注this 示例以从 Web 服务获取数据并将 Web 服务数据存储在字符串中。

但我想将此字符串转换为 json 并遵循一些示例,其中之一是 thisthis

所以我只是想知道我在这里做错了什么,或者这是使用它的正确方法,因为我不知道如何在Java中使用Web服务......

【问题讨论】:

标签: java json swing web-services httpurlconnection


【解决方案1】:

你需要先创建它的对象,然后你需要使用这个

JSONObject jsnobject = new JSONObject(String);

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

并确保在您的代码org.json.JSONObject 中导入这个

【讨论】:

    【解决方案2】:

    您可以按如下方式获取 JSONObject

        JSONObject jsonObj = new JSONObject(YOUR_STRING_OBJECT);
    

    查看This了解更多信息

    【讨论】:

    • 我已经使用了这个我有字符串而不是字符串对象..要使用这个我需要传递字符串对象
    • 唯一的问题是我没有导入正确的文件我需要导入“import org.json.JSONObject;”这...对不起,谢谢:)
    【解决方案3】:

    你可以使用google gson jar。

    Json Jar link

    并将对象转换为 json,如下所示。 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);
        }
    }
    

    使用 ajax 调用 Json。

     <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>
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-07-03
    • 1970-01-01
    • 1970-01-01
    • 2020-08-10
    • 2018-04-24
    • 2017-04-11
    • 1970-01-01
    相关资源
    最近更新 更多