我如何分离json数组,使它们有不同的变量

时间:2012-03-03 04:37:27

标签: android json

嘿,大家都有这个json代码

[{"page_no":"1","btn_one":"choice one p1","btn_two":"choice two p1","btn_three":"choice three p1","btn_four":"choice four p1","btn_five":"choice five p1","page":"this is the first page"},{"page_no":"2","btn_one":"choice 1 p2","btn_two":"choice 2 p2","btn_three":"choice 3 p2","btn_four":"choice 4 p2","btn_five":"choice 5 p2","page":"this is the second page"}]

我需要在文本视图和5 btns中显示,但我不知道如何分离两个json数组,以便我可以显示一个然后另一个

它显示在textview

this is the first pagethis is the second page

按钮也是如此。我需要它在文本视图中显示

this is the first page

然后当你点击一个按钮时,它应该刷新并显示

this is the second page

这是我完整编辑的代码

   public class MainActivity extends Activity {



//used to download
String result = "";
InputStream is = null;


String[] pageIndex = null;
String[] pageOne = null;
String[] choiceOne = null;
String[] choiceTwo = null;
String[] choiceThree = null;
String[] choiceFour = null;
String[] choiceFive = null;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);



    returnJson();


//end of onCreate()    
}

    public void returnJson(){
final TextView page = (TextView) findViewById(R.id.textView);
final Button btn1 = (Button) findViewById(R.id.btn1);
final Button btn2 = (Button) findViewById(R.id.btn2);
final Button btn3 = (Button) findViewById(R.id.btn3);
final   Button btn4 = (Button) findViewById(R.id.btn4);
final Button btn5 = (Button) findViewById(R.id.btn5);

    try{
            HttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost("http://10.0.2.2/textures_story_one.php");

            HttpResponse response = httpClient.execute(httpPost);
            HttpEntity entity = response.getEntity();
            is = entity.getContent();

    }catch(Exception e) {
        page.setText("error3");
    }

    try{


        BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"), 8);                      
        StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close();
            result = sb.toString();

    }catch(Exception e) {
        page.setText("error2");         
        }
    try{
    JSONArray jArray = new JSONArray(result);

    for(int i = 0;i<jArray.length();i++){

            pageIndex[i] = jArray.getJSONObject(i).getString("page_no"); 
            pageOne[i] = jArray.getJSONObject(i).getString("page"); 
            choiceOne[i] = jArray.getJSONObject(i).getString("btn_one");
            choiceTwo[i] = jArray.getJSONObject(i).getString("btn_two");  
            choiceThree[i] = jArray.getJSONObject(i).getString("btn_three");
            choiceFour[i] = jArray.getJSONObject(i).getString("btn_four"); 
            choiceFive[i] = jArray.getJSONObject(i).getString("btn_five");

    }
    page.setText(pageOne[0]);
    btn1.setText(choiceOne[0]);
    btn2.setText(choiceTwo[0]);
    btn3.setText(choiceThree[0]);
    btn4.setText(choiceFour[0]);
    btn5.setText(choiceFive[0]);

    }
catch(JSONException e) {
   // page.setText("error1");
}

// set the onClickListener for the button you want with the following code
btn1.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        page.setText(pageOne[1]);
        btn1.setText(choiceOne[1]);
        btn2.setText(choiceTwo[1]);
        btn3.setText(choiceThree[1]);
        btn4.setText(choiceFour[1]);
        btn5.setText(choiceFive[1]);
    }
});

return;


//end of returnJson()   
}


//end of class    
}

现在它抛出了几个错误,我不担心按钮atm,但我很高兴能够提供更多关于如何显示的帮助

this is the first page 
文本视图中的

1 个答案:

答案 0 :(得分:0)

该代码仅连接相同String对象中的两个页面/按钮。您应该使用单独的变量或数组,并在用户单击按钮时更改页面和每个按钮的文本。

使用您的代码,可能是这样的:

try{
    BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"), 8);                      
    StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
        is.close();
        result = sb.toString();

}catch(Exception e) {
    page.setText("error2");         
    }

try{
    JSONArray jArray = new JSONArray(result);

    for(int i = 0;i<jArray.length();i++){

            pageIndex[i] = jArray.getJSONObject(i).getString("page_no"); 
            pageOne[i] = jArray.getJSONObject(i).getString("page"); 
            choiceOne[i] = jArray.getJSONObject(i).getString("btn_one");
            choiceTwo[i] = jArray.getJSONObject(i).getString("btn_two");  
            choiceThree[i] = jArray.getJSONObject(i).getString("btn_three");
            choiceFour[i] = jArray.getJSONObject(i).getString("btn_four"); 
            choiceFive[i] = jArray.getJSONObject(i).getString("btn_five");

    }
    page.setText(pageOne[0]);
    btn1.setText(choiceOne[0]);
    btn2.setText(choiceTwo[0]);
    btn3.setText(choiceThree[0]);
    btn4.setText(choiceFour[0]);
    btn5.setText(choiceFive[0]);

}
catch(JSONException e) {
    page.setText("error1");
}

// set the onClickListener for the button you want with the following code
YOURBUTTON.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        page.setText(pageOne[1]);
        btn1.setText(choiceOne[1]);
        btn2.setText(choiceTwo[1]);
        btn3.setText(choiceThree[1]);
        btn4.setText(choiceFour[1]);
        btn5.setText(choiceFive[1]);
    }
});

return;

请注意,pageOnechoiceOnechoiceTwochoiceThreechoiceFourchoiceFive现在应该是字符串数组而不是字符串。< / p>