简体   繁体   中英

Easy way to get to values from JSONArray

I recently started working with JSON in Java. We have been setting and getting our values as follows from this JSONArray:

[{"productId":"1"},{"productName":"hammer"}]

JSONObject jo = ja.getJSONObject(0);

We could easily get the values by calling jo.getString("productId"); which would return the 1.

The problem is that sometimes we get different types of JSON objects. They look like this:

[{"name":"productId", "value":"1"},{"name":"productName", "value":"hammer"}]

Is there a way to easily eliminate those predicate name/value and just group the actual name and value together (as in the first example)?

The short answer is no. The longer answer is that you're not working with JSON, you're working with someone's misunderstanding of JSON.
Both of your examples look a bit like JSON, but they're both bogus.
[] is an array.
{} is an object.

Your first string [{"productId":"1"},{"productName":"hammer"}] is an array of two objects, where each object has one property. It's confusing to put dissimilar objects into an array together, but that's going on in both of your examples.
The second example [{"name":"productId", "value":"1"},{"name":"productName", "value":"hammer"}] shows an array of two objects, but again, the objects are dissimilar.
I think what they're going for is more like [{"productId":"1","productName":"hammer"}] , so I guess the long answer to your question is that you need to go to whomever is providing this "JSON" and tell them to fix it.

To give you a clearer idea of the correspondence between objects (in Java and otherwise) and JSON, check out the Java program below:

public class Product {
    String productName;
    String productId;
    public Product(String productId,String productName){
        this.productName = productName;
        this.productId = productId;
    }
    public String toString(){return toJSONString();}
    public String toJSONString(){
        return "{\"productId\":\""+productId+",\"productName:\""+productName+"\"}";
    }
    public static String arrayToJSONString(Product[] arry){
        StringBuilder sb = new StringBuilder();
        sb.append("["+arry[0]);
        for (int n =1;n<arry.length;n++){
            sb.append(","+arry[n]);
        }
        sb.append("]");
        return sb.toString();
    }
    public static void main(String [] args){
        Product p1 = new Product("1","hammer");
        Product[] arry = {p1};
        Product[] arry2 ={p1,new Product("2","shovel"), new Product("3","manure")};
        System.out.println("One object");
        System.out.println("    "+p1);
        System.out.println("An array containing one object");
        System.out.println("    "+Product.arrayToJSONString(arry));
        System.out.println("An array containing three objects");
        System.out.println("    "+Product.arrayToJSONString(arry2));
    }
}

Here's the output showing the proper JSON representation:

One object
{"productId":"1,"productName:"hammer"}
An array containing one object
[{"productId":"1,"productName:"hammer"}]
An array containing three objects
[{"productId":"1,"productName:"hammer"},{"productId":"2,"productName:"shovel"},{"productId":"3,"productName:"manure"}]

(Newlines are an artifact of the HTML, not JSON)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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