帮助解析简单的JSON(将JSON用于JAVA ME) [英] Help parsing simple JSON (using JSON for JAVA ME)

查看:116
本文介绍了帮助解析简单的JSON(将JSON用于JAVA ME)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是解析JSON的新手-到目前为止,我一直只是XML.无论如何,我正在使用JSON(Java ME)来解析具有以下结构的内容:

I new to parsing JSON - up until this point, I've been purely XML. Anyways, I am using JSON (Java ME) to parse something with the following structure:

{"name" : "JACK","name" : "JILL","name" : "JOHN","name" : "JENNY","name" : "JAMES","name" : "JIM"}

这是我的代码:

    try {
        JSONObject json = new JSONObject(response);
        JSONArray jsonArray = outer.getJSONArray("name");
        System.out.println("ARRAY SIZE:"
                + jsonArray.length());
    } catch (JSONException ex) {
    }

我的问题是我什至无法在我的Eclipse控制台中完全输出println("ARRAY SIZE:" ...)语句.我唯一获得任何形式的输出的都是我是否使用以下代码:

My problem is that I cannot even get the println("ARRAY SIZE:"...) statement to output at all in my Eclipse console. The only time that I am getting any sort of output is if I use the following code:

try {
        JSONObject json = new JSONObject(response);
        System.out.println("OUTPUT:"
                + json.getString("name"));
    } catch (JSONException ex) {
    }

...这似乎只给了我最后一个要素.我为什么无法使JSONArray正常工作,这是有原因的吗?是因为JSON不包含外"键吗?

...That seems to give me only the last element. Is there a reason why I cannot get the JSONArray to work? Is it because the JSON contains no "outer" key?

我将不胜感激.谢谢!

推荐答案

密钥必须唯一.您需要使用"Name1","Name2"等...来区分那些名称"

The key must be unique. You need to differentiate those "name"'s by using "Name1", "Name2", ... etc

这篇关于帮助解析简单的JSON(将JSON用于JAVA ME)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆