我如何从Android中的JSONArray访问数据?

时间:2015-08-13 10:35:01

标签: android json string

如何从JSONArray访问数据?就是这个并包含这些信息:

"deadlines": [
{
    "start": 1439539200,
    "end": 1439542800
},
{
    "start": 1440144000,
    "end": 1440147600
},
{
    "start": 0,
    "end": 0
}
]

我需要在String标记中添加每个项目"start"。感谢

修改

我的代码是:

JSONArray array = moduleObject.specialForcedConf;
// array = [{"deadlines":[{"start":1439539200,"end":1439542800},{"start":1440144000,"end":1440147600},{"start":0,"end":0}]}]

                    for (int j=0; j < array.length(); j++)
                    {
                        try
                        {
                            JSONObject obj = array.getJSONObject(j);
                            String start = obj.getString("start"); 
                            String end = obj.getString("end"); 

                            Log.e("", "start = " + start);
                        }
                        catch (JSONException e)
                        {
                            Log.e("", "error = " + e.getMessage());
                        }
                    }

我收到此错误:

"error = No value for start"

4 个答案:

答案 0 :(得分:0)

JSONArray ja = new JSONArray(yourjsondata));
for (int i = 0; i < ja.length(); i++) {
    JSONObject jo_feed = new JSONObject(ja.get(i).toString());
    String start = jo_feed.getString("start");
}

答案 1 :(得分:0)

这样做

Á2EKP"7Ô¢R]‹(ke

答案 2 :(得分:0)

试试这个:

std::ifstream myfile;
myfile.open( "D:\\values_user.txt", std::ios::in );

if( !(myfile.is_open()) )
{   std::cout << "Error Opening File";
std::exit(0);   }

std::string firstline;


while( myfile.good() )
{
    std::getline( myfile, firstline);
    std::cout<< "\n" << firstline <<"\n";
}

答案 3 :(得分:0)

JSONArray mJsonArray=new JSONArray("Deadlines");

for(int i=0;i<mJsonArray.length();i++){
 JSONObject mJsonObject=new JSONObject(mJsonArray.get(i).toString));
 String start = mJsonObject.optString("start",""); 
 String end = mJsonObject.optString("end",""); 
}