-1

JSON-简单示例

private static String jsonText = "{\"first\": \"ali\", \"second\":" +
                " [4, 5, 6], \"third\": 789},{\"first\": \"saeed\", \"second\":" +
                " [10, 7, 8], \"third\": 1000},{\"first\": \"reza\", \"second\":" +
                " [14, 15, 16], \"third\": 999}";

public static void main(String args[]) 
            throws org.json.simple.parser.ParseException {
    JSONParser parser = new JSONParser();
    ContainerFactory containerFactory = new ContainerFactory(){
        public List creatArrayContainer() {
            return new LinkedList();
        }

        public Map createObjectContainer() {
            return new LinkedHashMap();
        }
    };

    try {
        Map json = (Map)parser.parse(jsonText, containerFactory);
        Iterator iter = json.entrySet().iterator();
        System.out.println("==iterate result==");
        while(iter.hasNext()) {
            Map.Entry entry = (Map.Entry)iter.next();
            System.out.println(entry.getKey() + "=>" + entry.getValue());
        }

        System.out.println("==toJSONString()==");
        System.out.println(JSONValue.toJSONString(json));
    } catch(ParseException pe) {
        System.out.println(pe);
    }
}

这抛出一个异常

Unexpected token COMMA(,) at position 51.

是三条记录的jsonText内容,怎么读取三条记录?

4

2 回答 2

1

The simplest way to decode it is to use jackson. the code I give is written by groovy,but you can easily convert

    import com.fasterxml.jackson.databind.ObjectMapper;

    def s="""
    {
        "items": [
            {
                "first": "ali",
                "second": [
                    4,
                    5,
                    6
                ],
                "third": 789
            },
            {
                "first": "saeed",
                "second": [
                    10,
                    7,
                    8
                ],
                "third": 1000
            },
            {
                "first": "reza",
                "second": [
                    14,
                    15,
                    16
                ],
                "third": 999
            }
        ]
    }
    """

    ObjectMapper mapper=new ObjectMapper();
    def map=mapper.readValue(s,Map.class);
    println map

result:

 [items:[[first:ali, second:[4, 5, 6], third:789], [first:saeed, second:[10, 7, 8], third:1000], [first:reza, second:[14, 15, 16], third:999]]]

then just get the data like using Map(map is actually a Map object).

于 2013-09-27T16:18:44.673 回答
1

您的 JSON 无效。当我弄乱这样的东西时,我使用 jsonlint.com 来检查 json。这是一些有效 JSON 的示例(注意我删除了反斜杠)

{
    "items": [
        {
            "first": "ali",
            "second": [
                4,
                5,
                6
            ],
            "third": 789
        },
        {
            "first": "saeed",
            "second": [
                10,
                7,
                8
            ],
            "third": 1000
        },
        {
            "first": "reza",
            "second": [
                14,
                15,
                16
            ],
            "third": 999
        }
    ]
}
于 2013-09-27T15:34:09.453 回答