简体   繁体   English

如何从 json 中获取 int 的实际价值

[英]How to real value for int from json

In my project I have an int jsonobject by 20 value.在我的项目中,我有一个 20 值的int jsonobject To get the int value I used this:为了获得int值,我使用了这个:

model.setInt(jsonObject.getInt("price"));

Now it returns 0 value for price.现在它返回 0 值的价格。 How can I get real value?我怎样才能获得真正的价值?

Here is My out put Json:这是我的输出 Json:

      "data": [
    {
      "id": "571dd7817f8b9a1c190bbdb5",
      "goods": 0,
      "rate": {
        "r1": 0
      },
      "price": 20
     }

Please look at the example:请看例子:

String jsonData = "{\"count\": 20}";
JSONObject obj = new JSONObject(jsonData);
System.out.println("Count: " + obj.getInt("count"));

Your JSON is invalid.您的JSON无效。 you can use online tools like the jsonformatter to test the validity of your JSON .您可以使用jsonformatter等在线工具来测试JSON的有效性。

Maybe try something like:也许尝试这样的事情:

{
    "data": [
      {
        "id": "571dd7817f8b9a1c190bbdb5",
        "goods": 0,
        "rate": {"r1": 0},
        "price": 20
      }
    ]
}

Looking at your code, you may have better luck with this structure:看看你的代码,你可能对这个结构有更好的运气:

{
    "id": "571dd7817f8b9a1c190bbdb5",
    "goods": 0,
    "rate": {"r1": 0},
    "price": 20
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 getString(“ K”)是否能够从json对象提取int值? - Is getString(“K”) able to extract int value from json Object? 根据数据库记录(占位符)从动态 json object 中获取值(实际值)并用实际值替换占位符 - Getting values(real value) from dynamic json object according to database records(place holders) and replace placeholders with real value 如何从int []数组返回正确的值? - How to return the correct value from an int[] array? 如何从微调器获取int值 - How to get int value from spinner 如何从GpsDirectory获得纬度和经度的实际价值? - How do get real value of latitude and longitude from GpsDirectory? 如何从Java ArrayList对象转换为实际值 - How can I convert from java ArrayList object to real value 从Json到Java Object的转换,int变量默认赋值为0,如果没有传入JSON - Conversion from Json to Java Object, the int variable is by default assigned value 0, if not passed in JSON "如何从数值反序列化为 int(错误 - 没有从数值反序列化的 int\/Int 参数构造函数\/工厂方法)" - How to deserialize from Number value to int (error - no int/Int-argument constructor/factory method to deserialize from Number value) 如何使用GSON将DataTime Json转换为Epoch int - How to convert from DataTime Json to Epoch int using GSON 从JSON文件中获取一个int - Get an int from a JSON File
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM