如何为json解析创建模型类?

时间:2016-09-01 07:58:02

标签: java android json retrofit

我需要为以下类型的json创建模型类:

{
    "AdditinalInfo": [
        {
            "Tag": "ORDER",
            "Value": "[{\"EN_CODE\":\"8901233014951\",\"SKU_CODE\":\"1000003\",\"SKU_DESC\":\"5Star crunchy chocolate 33g\" ,\"QUANTITY\":\"1\",\"UNIT_SELLING_PRICE\":\"18.0\"}]"
        }
    ]
}

请帮助我如何为上面的json创建模型类。我需要使用POST方法发送json。

5 个答案:

答案 0 :(得分:4)

使用

http://www.jsonschema2pojo.org/

删除json,显示其通过json到json位置的虚拟副本

单击预览,然后最后下载zip

进行。

由于

答案 1 :(得分:1)

您可以使用THIS

等在线工具自动从json生成模型
-----------------------------------com.example.AdditinalInfo.java-----------------------------------

package com.example;

import javax.annotation.Generated;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

@Generated("org.jsonschema2pojo")
public class AdditinalInfo {

    @SerializedName("Tag")
    @Expose
    public String tag;
    @SerializedName("Value")
    @Expose
    public String value;

}
-----------------------------------com.example.Example.java-----------------------------------

package com.example;

import java.util.ArrayList;
import java.util.List;
import javax.annotation.Generated;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

@Generated("org.jsonschema2pojo")
public class Example {

    @SerializedName("AdditinalInfo")
    @Expose
    public List<AdditinalInfo> additinalInfo = new ArrayList<AdditinalInfo>();
}

答案 2 :(得分:0)

git checkout -b feature2 $(git merge-base --fork-point master feature1)

答案 3 :(得分:0)

您不需要创建通过Retrofit发送此Json的模型。

{
      "responseHeader":{
        "status":0,
        "QTime":0,
        "params":{
          "q":"*:*",
          "indent":"true",
          "wt":"json"}},
      "response":{"numFound":7,"start":0,"docs":[
          {
            "id":"1",
            "name":"Brian",
            "date":"2016-05-22",
            "age":"5",
            "state":null, //or blank, or anything really?
            "_version_":1540387858163957773}]
      }}

答案 4 :(得分:-1)

{}大括号用于对象,[]是数组。

例如,

    class ModelClass{ 

    public ArrayList<ADDITIONALINFOCLASS> AdditinalInfo;

    public class ADDITIONALINFOCLASS {
    public String Tag;
    public String Value;
   }
 }

您获得的错误是错误的解析,请尝试此代码并查看它是否有效。

相关问题