当前位置: 动力学知识库 > 问答 > 编程问答 >

java - How to parse json with fromJson method

问题描述:

I want to parse my json by fromJson class but I am unable to understand what should be the structure of my class if my json is like below:

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}]}}"

This is what I want to do

Response res = new Response();

Gson gson = new Gson();

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

I want to know what all variable should I take in Response class.

网友答案:
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");

A short example, you should see the pattern by now. Otherwise, just google forJSONObject and JSONArray

网友答案:
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");

Like the above you can get what ever you want in your jsonobject.

If you want to get anything from your JSONARRAY you need to in the following way.

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");
}

The above shown way you will get all the values from your JSONArray.

网友答案:

Here's a sample to get you started on unmarshalling the json string into an actual object structure. You'll get the idea.

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
    }
}
网友答案:

If you still want to stick to 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;
    }
}
网友答案:

Here m showing parse only upto token data from respons ::

jsonObject = new JSONObject(json);
jSONObject object = jsonObject.getJSONObject("Response");

String attr1 = object.getString("Token");
Log.i("value for token",""+object);
分享给朋友:
您可能感兴趣的文章:
随机阅读: