繁体   English   中英

Android,将JSON数组转换为ArrayList <String>

[英]Android, JSON array into ArrayList<String>


如何将JSON字符串数组解析为Java arraylist

我有这个json:

{
"a": [
    "A-lore ipsum 1",
    "A-lore ipsum 2",
    "A-lore ipsum 3"
],
"b": [
    "B-lore ipsum 1",       
    "B-lore ipsum 2",
    "B-lore ipsum 3"
]
}


这是我的原始代码:

//v refers to  **a or b**
public ArrayList getJSON(String v){
    ArrayList<String> list = new ArrayList<String>();
    InputStream is = getResources().openRawResource(R.raw.myjson);
    Writer writer = new StringWriter();
    char[] buffer = new char[1024];
    Reader reader = null;

    try {
        reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
        int n;
        while ((n = reader.read(buffer)) != -1) {
            writer.write(buffer, 0, n);
        }

        String json = writer.toString();
        JSONObject jsonObject = new JSONObject(json);
        list = jsonObject.getString(v); //how to create list according to category

    } catch (Exception e) {  e.printStackTrace();}

    return list;
}

所以我希望我的清单看起来像:
如果v ='a';

list = {"A-lore ipsum 1","A-lore ipsum 2","A-lore ipsum 3"};

如果v ='b';

list = {"B-lore ipsum 1","B-lore ipsum 2","B-lore ipsum 3"};
public class MainActivity extends ActionBarActivity {

    private String TAG = MainActivity.class.getSimpleName();

    private TextView textView;
    ArrayList<String> arrayListA = new ArrayList<String>();
    ArrayList<String> arrayListB = new ArrayList<String>();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        textView = (TextView) findViewById(R.id.textView);

        try {
            InputStream is = getAssets().open("myfile.json");

            int size = is.available();
            byte [] byteArray = new byte[size];

            is.read(byteArray);
            is.close();

            String json = new String(byteArray, "UTF-8");

            try {
                JSONObject jsonObject = new JSONObject(json);
                JSONArray arrayA = jsonObject.getJSONArray("a");
                JSONArray arrayB = jsonObject.getJSONArray("b");


                for (int i = 0 ; i < arrayA.length(); i++) {
                    String str = (String) arrayA.get(i);
                    arrayListA.add(str);
                }

                for (int i = 0 ; i < arrayB.length(); i++) {
                    String str = (String) arrayB.get(i);
                    arrayListB.add(str);
                }

                for (String str : arrayListA) 
                        Log.d(TAG, "strA : "+str);

                for (String str : arrayListB) 
                    Log.d(TAG, "strB : "+str);

            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }


        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

您在上面提供的json中有语法错误; a和b之间应该有一个逗号:

{
    "a": [
        "A-lore ipsum 1",
        "A-lore ipsum 2",
        "A-lore ipsum 3"
    ],
    "b": [
        "B-lore ipsum 1",
        "B-lore ipsum 2",
        "B-lore ipsum 3"
    ]
}

对于解析json,我建议使用Jackson(或Gson)而不是org.json库。 我发现它们可以更好地进行交互。

杰克逊Wiki位于http://wiki.fasterxml.com/JacksonHome ,其中包含许多教程等

使用杰克逊,它将是:

ObjectMapper mapper = new ObjectMapper();

final SomeClass someClass = mapper.readValue(json, SomeClass.class);

List<String> a = someClass.getA();

SomeClass在哪里

class SomeClass {
    @JsonProperty
    private List<String> a;

    @JsonProperty
    private List<String> b;

    public SomeClass() {
    }

    public List<String> getA() {
        return a;
    }

    public List<String> getB() {
        return b;
    }
}

Op更新了指定使用org.json的问题,并且需要返回数组或列表。

我强烈建议您使用功能更全面的json库(例如杰克逊)-参见Android,将JSON数组放入ArrayList <String>中 ,以了解实现此目的的杰克逊方法

下面将通过json简单实现您想要的功能,但是您可以看到使用org.json库引入了一些脆弱性:

final JSONObject jsonObject = new JSONObject(json);
final JSONArray a = (JSONArray) jsonObject.get("a");
final List<String> returnArray = new ArrayList<>();

for (int i = 0; i < a.length(); i++) {
    returnArray.add((String)a.get(i));
}

return returnArray;

暂无
暂无

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

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