简体   繁体   English

如何从Java中的json对象检索数据?

[英]How to retrieve data from json object in java?

I'm trying to retrieve Value ie, 80000 where Name is "Camera" from the Product's JSON using java. 我正在尝试使用java从产品的JSON中检索值(即80000 ,其中Name是“ Camera”)。 Can anyone please help me out ? 有人可以帮我吗?

{
   "Products":{  
      "Product":[  
         {  
            "Name":"Tv",
            "Value":50000
         },
         {  
            "Name":"Camera",
            "Value":80000
         },
         {  
            "Name":"Phone",
            "Value":15000
         },
         
      ]
   }
}

Mycode: mycode的:

JSONObject arrayOfProducts = jsonObj.optJSONObject("Products");
        JSONArray products = arrayOfProducts.getJSONArray("Product");
        for (int i = 0; i < products.length(); i++) {
            JSONObject objects = products.getJSONObject(i);
            Iterator key = objects.keys();
            while (key.hasNext()) {
                String k = key.next().toString();
                if(k.equals("Name")) {
                    if(objects.getString(k).equals("Camera")) {
                System.out.println("Key : " + k + ", value : " + objects.getString(k));
                    }
                }
            }

Your JSON is wrong. 您的JSON错误。

  • Don't use left/right quotes. 不要使用左/右引号。 Instead use quotes without a direction. 而是使用没有方向的引号。

Correct: "Products" Wrong: “Products” 正确: "Products" 错误: “Products”

  • Each JSON object should be enclosed in {} . 每个JSON对象都应包含在{} Enclose your whole JSON file content within {} . 将整个JSON文件内容包含在{}

If you want to get the value in a key-value pair in a JSON you need to use the following syntax: 如果要在JSON的键值对中获取值,则需要使用以下语法:

  • [JSONObject].get[data_type]([key_name]) . [JSONObject].get[data_type]([key_name])

    In the above given syntax, replace the [JSONObject] with the variable of type JSONObject representing the JSON. 在上面给出的语法中,将[JSONObject]替换为代表JSON的JSONObject类型的变量。 In your case, it is objects . 在您的情况下,它是objects

    replace [data_type] with the data type of the value at that particular key. [data_type]替换为该特定键上的值的数据类型。 In the current case, value for the key "Value" is 80000 is an Integer .. Hence it should be getInt . 在当前情况下,键"Value"80000是一个Integer ..因此,它应该是getInt

    Replace [key_name] with the key whose value you need to retrieve, which in your case is "Value" . [key_name]替换为您需要检索其值的键,在您的情况下为"Value"

Hence the code snippet you need to use to get the value 80000 which with the "Camera" part is: objects.getInt("Value") . 因此,您需要使用代码段来获取值80000,其中“ Camera”部分为: objects.getInt("Value")

Here is your overall updated code: 这是您的整体更新代码:

    JSONObject arrayOfProducts = jsonObj.optJSONObject("Products");
    JSONArray products = arrayOfProducts.getJSONArray("Product");
    for (int i = 0; i < products.length(); i++) {
        JSONObject objects = products.getJSONObject(i);
        Iterator key = objects.keys();
        while (key.hasNext()) {
            String k = key.next().toString();
            if(k.equals("Name")) {
                if(objects.getString(k).equals("Camera")) {
            System.out.println("\nKey : " + k + "\nName : " + objects.getString(k) + ", \nValue : " + objects.getInt("Value"));
                }
            }
        }
    }

Why you need to choose complex solution to parse JSON like that? 为什么您需要选择复杂的解决方案来解析JSON?

You should use define some entities and then use library for reading JSON value (Ex: "ObjectMapper" of jackson) to parser it to Object. 您应该使用定义一些实体,然后使用库来读取JSON值(例如:jackson的“ ObjectMapper”)以将其解析为Object。

You can leave all complex parts for them, after that you just working on java object. 您可以将所有复杂的部分留给它们,之后,您只需处理java对象。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM