如何在引号内用花括号解析json?

时间:2018-06-19 08:23:32

标签: java android json gson

如何解析这种json?

 {
  "tId": 5439661,
  "name": "aASD",
  "inputParameters": {
  "a": "50",
  "b": "234324",
  "c": "wefefew",
  "d": "T",
  "e": "4224",
  "f": "T",
  "g": "t"
  },
  "outputParameters": {
  "dId": "{1234435-A333F-A3334-A273-123243252355}",
  "fd": "1000023456"
  }
 }

解析代码:

JSONObject jsonObject = null;
try {
    jsonObject = new JSONObject(response.body().toString());
} catch (JSONException e) {
    e.printStackTrace();
}

我上面尝试了但是我收到了这个错误:

org.json.JSONException: Expected ':' after 1234435-A333F-A3334-A273-123243252355

我尝试使用POJO和gson库但仍然没有成功

有人解决过这种问题吗? 如何将“dId”作为字符串?

已编辑:已发布服务器的响应以进行测试

2 个答案:

答案 0 :(得分:0)

使用此型号: - 并使用Gson First Include库Gson之前使用它

dependencies {
               implementation 'com.google.code.gson:gson:2.8.2' // Old 2.8.0
             }
Gson gson = new Gson();
RootObject rootObject = new RootObject();

rootObject = gson.fromJson("Your json string or content", RootObject.class)

public class InputParameters
{
  private String a;

  public String getA() { return this.a; }

  public void setA(String a) { this.a = a; }

  private String b;

  public String getB() { return this.b; }

  public void setB(String b) { this.b = b; }

  private String c;

  public String getC() { return this.c; }

  public void setC(String c) { this.c = c; }

  private String d;

  public String getD() { return this.d; }

  public void setD(String d) { this.d = d; }

  private String e;

  public String getE() { return this.e; }

  public void setE(String e) { this.e = e; }

  private String f;

  public String getF() { return this.f; }

  public void setF(String f) { this.f = f; }

  private String g;

  public String getG() { return this.g; }

  public void setG(String g) { this.g = g; }
}

public class OutputParameters
{
  private String dId;

  public String getDId() { return this.dId; }

  public void setDId(String dId) { this.dId = dId; }

  private String fd;

  public String getFd() { return this.fd; }

  public void setFd(String fd) { this.fd = fd; }
}

public class RootObject
{
  private int tId;

  public int getTId() { return this.tId; }

  public void setTId(int tId) { this.tId = tId; }

  private String name;

  public String getName() { return this.name; }

  public void setName(String name) { this.name = name; }

  private InputParameters inputParameters;

  public InputParameters getInputParameters() { return this.inputParameters; }

  public void setInputParameters(InputParameters inputParameters) { this.inputParameters = inputParameters; }

  private OutputParameters outputParameters;

  public OutputParameters getOutputParameters() { return this.outputParameters; }

  public void setOutputParameters(OutputParameters outputParameters) { this.outputParameters = outputParameters; }
}

答案 1 :(得分:0)

写一下      String text= response.getString("dId");

然后文字将是:{1234-123423-53235423}你可以用这个文字做所有事情。

我用这个代码示例解决了你的问题,我的代码运行正常。

public void getjson() {

    RequestQueue mQueue;

    mQueue = Volley.newRequestQueue(MainActivity.this);

    String url = "http://www.mocky.io/v2/5b28ca862f00006300f55e6e";

    JsonObjectRequest request = new JsonObjectRequest(com.android.volley.Request.Method.GET, url, null, new com.android.volley.Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {
            try {
                Log.d("Response", response.toString());

                if(response.length()>0){
                    String id=response.getString("tId");
                    String name=response.getString("name");

                    Log.d("response.getid",id);
                    Log.d("response.getname",name);

                    JSONObject inputObject=response.getJSONObject("inputParameters");
                    if(inputObject.length()>0){
                        String a=inputObject.getString("a");
                        String b=inputObject.getString("b");
                        String c=inputObject.getString("c");
                        String d=inputObject.getString("d");
                        String e=inputObject.getString("e");
                        String f=inputObject.getString("f");
                        String g=inputObject.getString("g");
                    }

                    JSONObject outObject=response.getJSONObject("outputParameters");
                    if(outObject.length()>0){
                        String didText=outObject.getString("dId");
                        String fdText=outObject.getString("fd");

                        Log.d("response.getstring",didText);
                        Log.d("response.getstring",fdText);

                    }


                }

            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }, new com.android.volley.Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.d("Error", "Error de response");
            error.printStackTrace();
        }
    });

    mQueue.add(request);
}
相关问题