简体   繁体   English

将JsonObject数组转换为整数

[英]Convert JsonObject array to integer

I am getting the following response from my server: 我从服务器收到以下响应:

{"SubjectName":["Irish","Maths","English","Science","Religion","Geography"],
"SubjectID":[1,2,3,4,5,6]}

What I need to do is try separate the response into two separate arrays SubjectName and SubjectID. 我需要做的是尝试将响应分为两个单独的数组SubjectName和SubjectID。

Here is what I have: 这是我所拥有的:

 try {
            jsonArray = jsonObject.getJSONArray("SubjectName");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        SubjectName = new String[jsonArray.length()];
        for (int i = 0; i < jsonArray.length(); i++)
        {
            try {
                SubjectName[i] = jsonArray.getString(i);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

        try
        {
            jsonArray = jsonObject.getJSONArray("SUBJECTID");
            Log.v("Worked", "SubjectID is in ");

        } catch (JSONException e)
        {
            Log.v("failed","Not able to retrieve data");
            e.printStackTrace();
        }
        SubjectID = new int[jsonArray.length()];
        for (int i = 0; i < jsonArray.length(); i++)
        {

            try {
                SubjectID[i] = jsonArray.getInt(i);
            } catch (JSONException e)
            {
                e.printStackTrace();


            }
        }

The String seems to convert no problem and I can get the SubjectName but the SubjectID part seems to fail and I have no idea why. 字符串似乎没有问题,我可以获取SubjectName,但是SubjectID部分似乎失败了,我也不知道为什么。 I've checked and I can't seem to find anywhere that shows me how to parse an int from the jsonaray. 我检查了一下,似乎找不到任何地方向我展示如何从jsonaray解析一个int。

You are using wrong node name. 您使用了错误的节点名称。

Replace 更换

jsonArray = jsonObject.getJSONArray("SUBJECTID");

with

jsonArray = jsonObject.getJSONArray("SubjectID");

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

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