Android 使用特定结构创建JSON结果

Android 使用特定结构创建JSON结果,android,json,Android,Json,我正在开发一个Android应用程序,我需要创建一个Json结果,其中包含特定结构中的一些数据 这是我必须得到的: { "speedtest":"10Mbit/s", "httpRequest":[ { "url":"www.google.com", "statusCode":"200" }, { "url":"www.googl

我正在开发一个Android应用程序,我需要创建一个Json结果,其中包含特定结构中的一些数据

这是我必须得到的:

 {  
      "speedtest":"10Mbit/s",
      "httpRequest":[  
         {  
            "url":"www.google.com",
            "statusCode":"200"
         },
         {  
            "url":"www.google.com",
            "statusCode":"200"
         },
         {  
            "url":"www.google.com",
            "statusCode":"200"
         }
      ],
      "traceroute":[  
         {  
            "url":"www.google.com",
            "nodes":[  
               "45.34.222.11",
               "232.43.54.32"
            ]
         },
         {  
            "url":"www.google.com",
            "nodes":[  
               "45.34.222.11",
               "232.43.54.32"
            ]
         },
         {  
            "url":"www.google.com",
            "nodes":[  
               "45.34.222.11",
               "232.43.54.32"
            ]
         }
      ]
   }
这是我写的代码,但它不能正常工作,我不明白为什么

public class JsonBuilder {
    String LogTag = "LogTags";

    JsonBuilder(long downSpeed, ArrayList < String > ReqTest) {
        long ds = downSpeed;
        ArrayList < String > rt = ReqTest;

        JSONArray ja = null;
        JSONObject mainJson = new JSONObject();
        JSONObject speedData = new JSONObject();
        try {
            speedData.put("speedtest", ds+"Mbit/s");
        } catch (JSONException e) {
            Log.wtf(LogTag, "JSON exception SpeedTest", e);
        }

        JSONObject HttpData = new JSONObject();
        String[] req_split;

        for (int i = 0; i < rt.size(); i++) {
            req_split = rt.get(i).split(";", 2);
            try {
                HttpData.put("url", req_split[0]);
                HttpData.put("statusCode", req_split[1]);
            } catch (JSONException e) {
                Log.wtf(LogTag, "JSON exception", e);
            }
        }

         ja = new JSONArray();
         ja.put(HttpData);


        try {
            mainJson.put("httpRequest", ja);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        Log.wtf(LogTag, "JSON" + mainJson.toString());

    }
}
公共类JsonBuilder{
字符串LogTag=“LogTags”;
JsonBuilder(长下行速度,ArrayListReqTest){
长ds=下降速度;
ArrayListrt=ReqTest;
JSONArray ja=null;
JSONObject mainJson=新的JSONObject();
JSONObject speedData=新的JSONObject();
试一试{
speedData.put(“speedtest”,ds+“Mbit/s”);
}捕获(JSONException e){
wtf(LogTag,“JSON异常速度测试”,e);
}
JSONObject HttpData=新的JSONObject();
字符串[]需要拆分;
对于(int i=0;i

我已经创建了json的一部分,现在我不知道如何将它合并到所需的json中,main
JSONObject
包含
speedtest
httpRequest
JSONArray
,因此不要在
mainJson
JSONObject中添加
httpRequest
,而是将其添加到
speedData
JSONObject中,如下所示:

 speedData.put("httpRequest", ja);

现在使用
speedData
作为最终的JSON字符串,而不是
mainJson

更好、更简单的方法是创建如下三个实体类

import com.google.gson.annotations.Expose;

public class Example {

    @Expose(serialize = true)
    String speedtest;
    @Expose(serialize = true)
    List<httpRequest> httpRequest;
    @Expose(serialize = true)
    List<traceroute> traceroute;
}

public class httpRequest {
    @Expose(serialize = true)
    String url;
    @Expose(serialize = true)
    String statusCode;
}


public class traceroute {
    @Expose(serialize = true)
    String url;
    @Expose(serialize = true)
    List<String> nodes;
}
String json = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().serializeNulls().create().toJson(expamleClassObject);
使用以下链接使用Android studio在应用程序中导入gson库

compile 'com.google.code.gson:gson:2.3.1'

如果您想使用JSON对象,可以这样做

try {
                JSONObject mainObject = new JSONObject();

                JSONArray httpRequest = new JSONArray();
                    JSONObject httpRequestObject = new JSONObject();
                    httpRequestObject.put("url", "www.google.com");
                    httpRequestObject.put("url", "www.google.com");
                httpRequest.put(httpRequestObject);

                JSONArray traceroute = new JSONArray();
                    JSONObject tracerouteObject = new JSONObject();
                    tracerouteObject.put("url", "www.google.com");
                        JSONArray nodes=new JSONArray();
                        nodes.put("45.34.222.11");
                        nodes.put("232.43.54.32");

                mainObject.put("speedtest","10Mbit/s");
                mainObject.put("httpRequest",httpRequest);
                mainObject.put("traceroute",traceroute);
                Log.e("json",mainObject.toString());

            }catch (Exception e){

            }

但是它不能正常工作,我不明白为什么你会遇到这样的问题?我需要创建这样的结构:{“speedtest”:“10Mbit/s”,“httpRequest”:[{“url”:“www.google.com”,“statusCode”:“200”},{“url”:“www.google.com”,“statusCode”:“200”},{“url”:“www.google.com”,“statusCode”:“200”}我不知道如何将speedData添加到主对象以创建最终的JSONDo speedData.put(“httpRequest”,ja);如果您的问题得到解决,那么请回答这个问题,以便其他人能够轻松解决这些类型的问题。如果我理解,我应该在项目中导入gson库,创建一个包含您的代码的java类(3个类),并使用它来“构建”“实时我的json,对吗?好的,我已经用你的代码创建了一个java类,现在我在理解如何使用它时遇到了一个问题,你能给我举个例子吗?(我从来没有使用过gson)使用您的代码,我可以获得我在邮件中编写的json吗?您使用的是android studio吗?在build.gradle文件的dependencies标记下编写compile'com.google.code.gson:gson:2.3.1'。