如何使用fromJson方法解析json

时间:2012-10-18 10:41:48

标签: java android json

我想通过fromJson类来解析我的json,但如果我的json如下所示,我无法理解我的类的结构应该是什么:

String json = "{"Result":{"Status":"SUCCESS","Message":""},"Response":{"Token":"ca0d7507-3907-4eed-af19-ad256bc71088","ZoneUrl":"https:\/\/qa.tritononline.com\/","CustomerID":1,"UserID":29,"DefaultLanguageID":1,"ZoneID":1,"IsTritonIntegrated":false,"Language":[{"LanguageId":1,"Language":"English","IsSelected":false}]}}"

这就是我想做的事情

Response res = new Response();
        Gson gson = new Gson();
        res = gson.fromJson(json, Response.class);

我想知道我应该在Response类中使用的所有变量。

5 个答案:

答案 0 :(得分:2)

String data = yourdata;
JSONObject jsonData = new JSONObject(data);
JSONOBject jsonResult = new JSONObject(jsonData.getString("Result"));
JSONOBject jsonResponse = new JSONObject(jsonData.getString("Response"));
String status = jsonResult.getString("Status");

一个简短的例子,你现在应该看到这个模式。否则,只需谷歌JSONObjectJSONArray

答案 1 :(得分:1)

String json = "{"Result":{"Status":"SUCCESS","Message":""},"Response":{"Token":"ca0d7507-3907-4eed-af19-ad256bc71088","ZoneUrl":"https:\/\/qa.tritononline.com\/","CustomerID":1,"UserID":29,"DefaultLanguageID":1,"ZoneID":1,"IsTritonIntegrated":false,"Language":[{"LanguageId":1,"Language":"English","IsSelected":false}]}}"

String[] langid=null;

JSONObject object;
JSONObject resultobj = new JSONObject(json);

object = resultobj;

JSONObject responseobj = object.getJSONObject("Response");
responseobj.getString("UserID");
  

如上所述,您可以在jsonobject中获得您想要的内容。

     

如果你想从你的JSONARRAY获得任何东西,你需要在   以下方式。

JSONArray languageobj = object.getJSONArray("Language");

langid = new String[languageobj.length()];

for(int i=0;i<language.length();i++)
{
JSONObject e = languageobj.getJSONObject(i);
langid[i] = e.getString("LanguageId");
}
  

以上显示的方式,您将获得JSONArray中的所有值。

答案 2 :(得分:1)

这是一个让您开始将json字符串解组为实际对象结构的示例。你会明白的。

import com.google.gson.Gson;
import com.google.gson.annotations.SerializedName;

// getters and setter omitted for brevity
public class Main {

    public static void main(String[] args) {
        String json = "{\"Result\":{\"Status\":\"SUCCESS\",\"Message\":\"\"},\"Response\":{\"Token\":\"ca0d7507-3907-4eed-af19-ad256bc71088\",\"ZoneUrl\":\"https://qa.tritononline.com/\",\"CustomerID\":1,\"UserID\":29,\"DefaultLanguageID\":1,\"ZoneID\":1,\"IsTritonIntegrated\":false,\"Language\":[{\"LanguageId\":1,\"Language\":\"English\",\"IsSelected\":false}]}}";
        Gson gson = new Gson();
        ServerResponse sr = gson.fromJson(json, ServerResponse.class);
        // do stuff with sr
    }

    public static class ServerResponse {
        public Result Result;
        public Response Response;
    }

    public static class Result {
        public String Status;

        // if you want to follow java conventions (lowercase field name) then
        // you need to add this annotation
        @SerializedName("Message")
        public String message;
    }

    public static class Response {
        public String Token;
        public String ZoneUrl;
        // ... etc
    }
}

答案 3 :(得分:1)

如果你还想坚持使用gson

        MyType fromJson = gsonHelper.fromJson("{'Result':{'Status':'SUCCESS','Message':''},'Response':{'Token':'ca0d7507-3907-4eed-af19-ad256bc71088','ZoneUrl':'https://qa.tritononline.com/','CustomerID':1,'UserID':29,'DefaultLanguageID':1,'ZoneID':1,'IsTritonIntegrated':false,'Language':[{'LanguageId':1,'Language':'English','IsSelected':false}]}}", MyType.class);
    System.out.println(fromJson.getResponse().getToken());
    System.out.println(fromJson.getResponse().getZoneUrl());

public class MyType
{
    private MyResult Result;
    private MyResponse Response;
    public MyResult getResult()
    {
        return Result;
    }
    public void setResult(MyResult result)
    {
        Result = result;
    }
    public MyResponse getResponse()
    {
        return Response;
    }
    public void setResponse(MyResponse response)
    {
        Response = response;
    }
}



public class MyResult
{
    private String Status;
    private String Message;
    public String getStatus()
    {
        return Status;
    }
    public void setStatus(String status)
    {
        Status = status;
    }
    public String getMessage()
    {
        return Message;
    }
    public void setMessage(String message)
    {
        Message = message;
    }
}




public class MyResponse
{
    private String Token;
    private String ZoneUrl;
    public String getToken()
    {
        return Token;
    }
    public void setToken(String token)
    {
        Token = token;
    }
    public String getZoneUrl()
    {
        return ZoneUrl;
    }
    public void setZoneUrl(String zoneUrl)
    {
        ZoneUrl = zoneUrl;
    }
}

答案 4 :(得分:1)

这里显示只解析来自respons ::

的令牌数据
jsonObject = new JSONObject(json);
jSONObject object = jsonObject.getJSONObject("Response");

String attr1 = object.getString("Token");
Log.i("value for token",""+object);