Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用java访问json_Java_Json - Fatal编程技术网

如何使用java访问json

如何使用java访问json,java,json,Java,Json,我在rest响应中有以下json: { "TRANS": { "HPAY": [ { "ID": "1234", "DATE": "10/09/2011 18:09:27", "REC": "wallet Ricaricato", "COM": "Tasso Commissione", "MSG": "Commento White

我在rest响应中有以下json:

{
      "TRANS": {
        "HPAY": [
          {
            "ID": "1234",
            "DATE": "10/09/2011 18:09:27",
            "REC": "wallet Ricaricato",
            "COM": "Tasso Commissione",
            "MSG": "Commento White Brand",
            "STATUS": "3",
            "EXTRA": {
              "IS3DS": "0",
              "CTRY": "FRA",
              "AUTH": "455622"
            },
            "INT_MSG": "05-00-05 ERR_PSP_REFUSED",
            "MLABEL": "IBAN",
            "TYPE": "1"
          }
        ]
      }
    }
我已经制作了pojo类来用java映射这个json

public class Trans {

    private List<Hpay> hpay;

    public Trans(){

    }
//getter and setter
}

如果我在事务上调用hpay方法,我就有空。我不知道为什么…

首先使用json.simple解析json数据,然后使用setter设置值

 Object obj = parser.parse(new FileReader( "file.json" ));
   JSONObject jsonObject = (JSONObject) obj;

   JSONArray hpayObj= (JSONArray) jsonObject.get("HPAY");

    //get the first element of array
    JSONObject details= hpayObj.getJSONArray(0);

    String id = (String)details.get("ID");
        //set the value of the id field in the setter of class Trans
       new Trans().setId(id);
       new Trans().setDate((String)details.get("DATE"));
       new Trans().setRec((String)details.get("REC"));
等等

 //get the second array element
      JSONObject intMsgObj= hpayObj.getJSONArray(1);
      new Trans().setIntmsg((String)details.get("INT_MSG"));

  //get the third array element
   JSONObject mlabelObj= hpayObj.getJSONArray(2);
      new Trans().setMlabel((String)details.get("MLABEL"));

JSONObject typeObj= hpayObj.getJSONArray(3);
 new Trans().setType((String)details.get("TYPE"));

现在您可以使用您的GETTER方法获取值。

我已经删除了以前的答案,并添加了一个新的值作为您的要求

JSON字符串:

{
    "TRANS": {
        "HPAY": [{
                "ID": "1234",
                "DATE": "10/09/2011 18:09:27",
                "REC": "wallet Ricaricato",
                "COM": "Tasso Commissione",
                "MSG": "Commento White Brand",
                "STATUS": "3",
                "EXTRA": {
                    "IS3DS": "0",
                    "CTRY": "FRA",
                    "AUTH": "455622"
                },
                "INT_MSG": "05-00-05 ERR_PSP_REFUSED",
                "MLABEL": "IBAN",
                "TYPE": "1"
            }
        ]
    }
}
Java对象:(此处
Extra
不是列表)

这里我使用
googlegosn
lib进行转换

并且需要导入下面的类进行注释

com.google.gson.annotations.Expose

com.google.gson.annotations.SerializedName

在Gson库中使用jackson。。他们有将JSON转换为java对象的实用工具。
Extra
class的定义是什么?可能是@Badda的重复我认为我的问题是我的类中的列表…在该代码中,你没有错过类定义中的“REC”字段你非常欢迎。。。如果有任何查询,我知道。我是否必须更改我的get/set方法?set/get方法的名称无关紧要。它适用于注释。如果您的json字段是get change,那么您需要更改
SerializedName
中的测试,例如:
@SerializedName(“”)
,现在我对另一个json也有同样的问题。。这里是哪种结构:我使用了相同的结构,但不起作用:(
 //get the second array element
      JSONObject intMsgObj= hpayObj.getJSONArray(1);
      new Trans().setIntmsg((String)details.get("INT_MSG"));

  //get the third array element
   JSONObject mlabelObj= hpayObj.getJSONArray(2);
      new Trans().setMlabel((String)details.get("MLABEL"));

JSONObject typeObj= hpayObj.getJSONArray(3);
 new Trans().setType((String)details.get("TYPE"));
{
    "TRANS": {
        "HPAY": [{
                "ID": "1234",
                "DATE": "10/09/2011 18:09:27",
                "REC": "wallet Ricaricato",
                "COM": "Tasso Commissione",
                "MSG": "Commento White Brand",
                "STATUS": "3",
                "EXTRA": {
                    "IS3DS": "0",
                    "CTRY": "FRA",
                    "AUTH": "455622"
                },
                "INT_MSG": "05-00-05 ERR_PSP_REFUSED",
                "MLABEL": "IBAN",
                "TYPE": "1"
            }
        ]
    }
}
public class MyObject {
    @SerializedName("TRANS")
    @Expose
    private Trans trans;

    public Trans getTRANS() {return trans;}

    public void setTRANS(Trans trans) {this.trans = trans;}
}

public class Trans {
    @SerializedName("HPAY")
    @Expose
    private List<HPay> hPay;

    public List<HPay> getHPAY() {return hPay;}

    public void setHPAY(List<HPay> hPay) {this.hPay = hPay;}
}

public class HPay {
    @SerializedName("ID")
    @Expose
    private String id;
    @SerializedName("DATE")
    @Expose
    private String date;
    @SerializedName("REC")
    @Expose
    private String rec;
    @SerializedName("COM")
    @Expose
    private String com;
    @SerializedName("MSG")
    @Expose
    private String msg;
    @SerializedName("STATUS")
    @Expose
    private String status;
    @SerializedName("EXTRA")
    @Expose
    private Extra extra;
    @SerializedName("INT_MSG")
    @Expose
    private String intMsg;
    @SerializedName("MLABEL")
    @Expose
    private String mLabel;
    @SerializedName("TYPE")
    @Expose
    private String type;

    public String getID() {return id;}
    public void setID(String id) {this.id = id;}
    public String getDATE() {return date;}
    public void setDATE(String date) {this.date = date;}
    public String getREC() {return rec;}
    public void setREC(String rec) {this.rec = rec;}
    public String getCOM() {return com;}
    public void setCOM(String com) {this.com = com;}
    public String getMSG() {return msg;}
    public void setMSG(String msg) {this.msg = msg;}
    public String getSTATUS() {return status;}
    public void setSTATUS(String status) {this.status = status;}
    public Extra getEXTRA() {return extra;}
    public void setEXTRA(Extra extra) {this.extra = extra;}
    public String getINTMSG() {return intMsg;}
    public void setINTMSG(String intMsg) {this.intMsg = intMsg;}
    public String getMLABEL() {return mLabel;}
    public void setMLABEL(String mLabel) {this.mLabel = mLabel;}
    public String getTYPE() {return type;}
    public void setTYPE(String type) {this.type = type;}
}

public class Extra {
    @SerializedName("IS3DS")
    @Expose
    private String is3ds;
    @SerializedName("CTRY")
    @Expose
    private String ctry;
    @SerializedName("AUTH")
    @Expose
    private String auth;

    public String getIS3DS() { return is3ds; }
    public void setIS3DS(String is3ds) { this.is3ds = is3ds; }
    public String getCTRY() { return ctry; }
    public void setCTRY(String ctry) { this.ctry = ctry; }
    public String getAUTH() { return auth; }
    public void setAUTH(String auth) { this.auth = auth; }
}
import com.google.gson.Gson;
    public class NewClass {
    public static void main(String[] args) {
        Gson g = new Gson();

        g.fromJson(json, MyObject.class);

    }

    static String json = "{ \"TRANS\": { \"HPAY\": [{ \"ID\": \"1234\", \"DATE\": \"10/09/2011 18:09:27\", \"REC\": \"wallet Ricaricato\", \"COM\": \"Tasso Commissione\", \"MSG\": \"Commento White Brand\", \"STATUS\": \"3\", \"EXTRA\": { \"IS3DS\": \"0\", \"CTRY\": \"FRA\", \"AUTH\": \"455622\" }, \"INT_MSG\": \"05-00-05 ERR_PSP_REFUSED\", \"MLABEL\": \"IBAN\", \"TYPE\": \"1\" } ] } }";
}