无法将Android中的JSON从WebApi转换为Json对象

时间:2022-02-11 21:31:15

i'm currently developing and android app that collects data from ASP.NET WebApi Service. The service works but i can't get the JSON object in android,DO i have to convert to JSON array or to JSON object, i have tried alot of answers out there but no success. Here is my server side code snippet:

我目前正在开发和Android应用程序,从ASP.NET WebApi服务收集数据。服务工作,但我不能在Android中获取JSON对象,我必须转换为JSON数组或JSON对象,我已经尝试了很多答案,但没有成功。这是我的服务器端代码片段:

 // GET api/uconnectservice
        public String Get()
        {

            var json = JsonConvert.SerializeObject(

                sampledata.GetAllDummy()
            );
            return json;

            //return sampledata.GetAll();
        }

which gives :

这使 :

    <string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">
    [{"AccountType":"15516321","CustomerName":"1MACMONSAM NICOLAS","Currentbalance":"1100,000.00","AllTransactions":[{"ID":103,"DescriptionoOfTransaction":"I need money right now","Debit":"5001","Credit":"0","TransactionDate":"15/12/2016 22:35:03","ValueDate":"15/12/2016 22:35:03"},{"ID":105,"DescriptionoOfTransaction":"I need money right now","Debit":"5002","Credit":"0","TransactionDate":"16/12/2016 22:35:03","ValueDate":"16/12/2016 22:35:03"},{"ID":107,"DescriptionoOfTransaction":"I need money right now","Debit":"5003","Credit":"0","TransactionDate":"17/12/2016 22:35:03","ValueDate":"17/12/2016 22:35:03"}......MORE JSON.........{"ID":113,"DescriptionoOfTransaction":"I need money right now","Debit":"5006","Credit":"0","TransactionDate":"20/12/2016 22:35:03","ValueDate":"20/12/2016 22:35:03"},{"ID":115,"DescriptionoOfTransaction":"I need money right now","Debit":"5007","Credit":"0","TransactionDate":"21/12/2016 22:35:03","ValueDate":"21/12/2016 22:35:03"},{"ID":117,"DescriptionoOfTransaction":"I need money right now","Debit":"5008","Credit":"0","TransactionDate":"22/12/2016 22:35:03","ValueDate":"22/12/2016 22:35:03"},{"ID":119,"DescriptionoOfTransaction":"I need money right now","Debit":"5009","Credit":"0","TransactionDate":"23/12/2016 22:35:03","ValueDate":"23/12/2016 22:35:03"},{"ID":121,"DescriptionoOfTransaction":"I need money right now","Debit":"50010","Credit":"0","TransactionDate":"24/12/2016 22:35:03","ValueDate":"24/12/2016 22:35:03"}]}]
</string>

etc which is fine

等等很好

On the android side where i have to consume this service here is my code:

在Android端,我必须在这里使用此服务是我的代码:

String URL = "http://192.168.1.101/UnicsApplication/api/uconnectservice";

    JSONObject jsonObject;
    private final OkHttpClient client = new OkHttpClient();

    public void run() throws Exception {
        Request request = new Request.Builder().url(URL).build();

        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                e.printStackTrace();

            }
try (ResponseBody responseBody = response.body()) {

                    if (!response.isSuccessful())

                        throw new IOException("Unexpected code " + response);

                    Headers responseHeaders = response.headers();

                    for (int i = 0, size = responseHeaders.size(); i < size; i++) {

                        Log.d("Results", responseHeaders.name(i) + ": " + responseHeaders.value(i));

                    }
                    //Log.d("Results", responseBody.string());

                    try {
                        //get JSON objetc first 

                        jsonObject = new JSONObject(responseBody.string());

                        Log.d("JsonObject", jsonObject.toString());

                    } catch (JSONException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }

The e.printstackTrance method is called.Error message i keep getting from Logcat is as follows:

调用e.printstackTrance方法。我从Logcat获取的错误消息如下:

12-15 01:59:44.280: W/System.err(26192): org.json.JSONException: Value {"operations":[{"AccountType":"15516321","CustomerName":"1MACMONSAM NICOLAS","Currentbalance":"1100,000.00","AllTransactions":[{"ID":103,"DescriptionoOfTransaction":"I need money right now","Debit":"5001","Credit":"0","TransactionDate":"16/12/2016 01:59:45","ValueDate":"16/12/2016 01:59:45"},{"ID":105,"DescriptionoOfTransaction":"I need money right now","Debit":"5002","Credit":"0","TransactionDate":"17/12/2016 01:59:45","ValueDate":"17/12/2016 01:59:45"},{"ID":107,"DescriptionoOfTransaction":"I need money right now","Debit":"5003","Credit":"0","TransactionDate":"18/12/2016 01:59:45","ValueDate":"18/12/2016 01:59:45"},{"ID":109,"DescriptionoOfTransaction":"I need money right now","Debit":"5004","Credit":"0","TransactionDate":"19/12/2016 01:59:45","ValueDate":"19/12/2016 01:59:45"},{"ID":111,"DescriptionoOfTransaction":"I need money right now","Debit":"5005","Credit":"0","TransactionDate":"20/12/2016 01:59:45","ValueDate":"20/12/2016 01:59:45"},{"ID":113,"DescriptionoOfTransaction":"I need money right now","Debit":"5006","Credit":"0","TransactionDate":"21/12/2016 01:59:45","ValueDate":"21/12/2016 01:59:45"},{"ID":115,"DescriptionoOfTransaction":"I need money right now","Debit":"5007","Credit":"0","TransactionDate":"22/12/2016 01:59:45","ValueDate":"22/12/2016 01:59:45"},{"ID":117,"DescriptionoOfTransaction":"I need money right now","Debit":"5008","Credit":"0","TransactionDate":"23/12/2016 01:59:45","ValueDate":"23/12/2016 01:59:45"},{"ID":119,"DescriptionoOfTransaction":"I need money right now","Debit":"5009","Credit":"0","TransactionDate":"24/12/2016 01:59:45","ValueDate":"24/12/2016 01:59:45"},{"ID":121,"DescriptionoOfTransaction":"I need money right now","Debit":"50010","Credit":"0","TransactionDate":"25/12/2016 01:59:45","ValueDate":"25/12/2016 01:59:45"},{"ID":103,"DescriptionoOfTransaction":"I'am putting in money right now","Debit":"15001","Credit":"0","TransactionDate":"16/12/2016 01:59:45","ValueDate":"16/12/2016 01:59:45"},{"ID":105,"DescriptionoOfTransaction":"I'am putting in money right now","Debit":"15002","Credit":"0","TransactionDate":"17/12/2016 01:59:45","ValueDate":"17/12/2016 01:59:45"},{"ID":107,"DescriptionoOfTransaction":"I'am putting in money right now","Debit":"15003","Credit":"0","TransactionDate":"18/12/2016 01:59:45","ValueDate":"18/12/2016 01:59:45"},{"ID":109,"DescriptionoOfTransaction":"I'am putting in money right now","Debit":"15004","Credit":"0","TransactionDate":"19/12/2016 01:59:45","ValueDate":"19/12/2016 01:59:45"},{"ID":111,"DescriptionoOfTransaction":"I'am putting in money right now","Debit":"15005","Credit":"0","TransactionDate":"20/12/2016 01:59:45","ValueDate":"20/12/2016 01:59:45"},{"ID":113,"DescriptionoOfTransaction":"I'am putting in money right now","Debit":"15006","Credit":"0","TransactionDate":"21/12/2016 01:59:45","ValueDate":"21/12/2016 01:59:45"},{"ID":115,"DescriptionoOfTransaction":"I'am putting in money right now","Debit":"15007","Credit":"0","TransactionDate":"22/12/2016 01:59:45","ValueDate":"22/12/2016 01:59:45"},{"ID":117,"DescriptionoOfTransaction":"I'am putting in money right now","Debit":"15008","Credit":"0","TransactionDate":"23/12/2016 01:59:45","ValueDate":"23/12/2016 01:59:45"},{"ID":119,"DescriptionoOfTransaction":"I'am putting in money right now","Debit":"15009","Credit":"0","TransactionDate":"24/12/2016 01:59:45","ValueDate":"24/12/2016 01:59:45"},{"ID":121,"DescriptionoOfTransaction":"I'am putting in money right now","Debit":"150010","Credit":"0","TransactionDate":"25/12/2016 01:59:45","ValueDate":"25/12/2016 01:59:45"},{"ID":103,"DescriptionoOfTransaction":"I need money right now","Debit":"5001","Credit":"0","TransactionDate":"16/12/2016 01:59:45","ValueDate":"16/12/2016 01:59:45"},{"ID":105,"DescriptionoOfTransaction":"I need money right now","Debit":"5002","Credit":"0","TransactionDate":"17/12/2016 01:59:45","ValueDate":"17/12/2016 01:59:45"},{"ID":107,"DescriptionoOfTransaction":"I need money right now","Debit":"5003","Credit":"0","TransactionDate":"18/12/2016 01:59:45","ValueDate":"18/12/20
12-15 01:59:44.280: W/System.err(26192):    at org.json.JSON.typeMismatch(JSON.java:111)
12-15 01:59:44.280: W/System.err(26192):    at org.json.JSONObject.<init>(JSONObject.java:159)
12-15 01:59:44.280: W/System.err(26192):    at org.json.JSONObject.<init>(JSONObject.java:172)
12-15 01:59:44.280: W/System.err(26192):    at com.nickSoft.Connections.OkHttpHandlerIncoming$1.onResponse(OkHttpHandlerIncoming.java:67)
12-15 01:59:44.280: W/System.err(26192):    at okhttp3.RealCall$AsyncCall.execute(RealCall.java:126)
12-15 01:59:44.280: W/System.err(26192):    at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
12-15 01:59:44.280: W/System.err(26192):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
12-15 01:59:44.280: W/System.err(26192):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
12-15 01:59:44.280: W/System.err(26192):    at java.lang.Thread.run(Thread.java:841)

Is this a JSON string? what can i do to get JSON object which will be later converted to a collection?.Please any guidance or help will greatly appreciated. Thnx.

这是一个JSON字符串吗?我该怎么做才能获得稍后将转换为集合的JSON对象?。任何指导或帮助都将非常感激。日Thnx。

1 个解决方案

#1


0  

The <string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">

is not valid JSON, looks like you got first some xml that contains json. My suggestion is to strip it down correctly on the server side. If that is not possible remove the string tags from the response before you start parsing

是无效的JSON,看起来你先得到一些包含json的xml。我的建议是在服务器端正确删除它。如果不可能,则在开始解析之前从响应中删除字符串标记

#1


0  

The <string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">

is not valid JSON, looks like you got first some xml that contains json. My suggestion is to strip it down correctly on the server side. If that is not possible remove the string tags from the response before you start parsing

是无效的JSON,看起来你先得到一些包含json的xml。我的建议是在服务器端正确删除它。如果不可能,则在开始解析之前从响应中删除字符串标记