从json响应中获取特定键的值

时间:2020-04-10 12:13:29

标签: android json kotlin gson

如何将json响应中键的特定值存储到变量中

{
   "results": [
    {
      "name": ryan,
      "roll_id": 64,
      "class_id": 310,
      "net_id": 95,
    },
  ],
};

上面是json响应:-

val gson = GsonBuilder().create()
val ListV = gson.fromJson(body, HomeClass::class.java)

在这两行之后,我完全不知道该怎么做,因为我已经上网了,但是我很难理解该如何继续。

1 个答案:

答案 0 :(得分:0)

您的Json结构将是

data class HomeClass (

    @SerializedName("results") val results : List<Results>
)

data class Results (

    @SerializedName("name") val name : String,
    @SerializedName("roll_id") val roll_id : Int,
    @SerializedName("class_id") val class_id : Int,
    @SerializedName("net_id") val net_id : Int
)

数据类应为

val listData = gson.fromJson(jsonData, HomeClass::class.java)

来自Json

 val totalSize = 0 until listData!!.size
  if(totalSize.size>0)
    {
        for (i in totalSize)
        {
                 //Your Code i==Position
        }
     }

然后

def fun(kier, arg):
    '''
    Function to fit
    :param kier: list of parameters
    :param arg: argument of the function fun
    :return y: value of function in x
    '''

    y =kier[0]*arg +1 #+kier[1]
    return y

dane = RealData(x=zx, y=zy, sx=dx, sy=dy)
model = Model(fun)
#kier,cross,none,none,none=linregress(zx_mean,zy_mean) #this is a method used to estimate the parameters, replaced by curve_fit.
popt, pcov = curve_fit(fun, zx, zy)
on = ODR(dane, model,beta0=[popt[0]])
output = on.run()