如何只从json获取特定值?

时间:2017-12-25 09:43:21

标签: android mysql json

我只需要帮助我的json数据实际上我的mysql表包含很多字段,我知道如何以json格式转换整个数据并将其解析为android应用程序但是我想根据条件来获取特定的数据行其中id = 1,name =" some",这是我迄今为止尝试过的,但没有成功。

     public class Result_1 extends AppCompatActivity {

TextView winning,rank,accuracy,team1_judge_score,team1_actual_score,team1_score_accuracy,
        team1_judge_fifty,team1_actual_fifty,team1_fifty_accuracy,
        team1_judge_wicket,team1_actual_wicket,team1_wicket_accuracy,
        team2_judge_score,team2_actual_score,team2_score_accuracy,
        team2_judge_fifty,team2_actual_fifty,team2_fifty_accuracy,
        team2_judge_wicket,team2_actual_wicket,team2_wicket_accuracy;


private ProgressDialog loading;
String mail;



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


     rank=(TextView)findViewById(R.id.rank);
     accuracy=(TextView)findViewById(R.id.Accuracy);
     //Winning=(TextView)findViewById(R.id.money);
     team1_judge_score=(TextView)findViewById(R.id.textView22);
     team1_actual_score=(TextView)findViewById(R.id.textView23);
     team1_score_accuracy=(TextView)findViewById(R.id.textView24);
     team1_judge_fifty=(TextView)findViewById(R.id.textView32);
     team1_actual_fifty=(TextView)findViewById(R.id.textView33);
     team1_fifty_accuracy=(TextView)findViewById(R.id.textView34);
     team1_judge_wicket=(TextView)findViewById(R.id.textView42);
     team1_actual_wicket=(TextView)findViewById(R.id.textView43);
     team1_wicket_accuracy=(TextView)findViewById(R.id.textView44);
     team2_judge_score=(TextView)findViewById(R.id.textView22_2);
     team2_actual_score=(TextView)findViewById(R.id.textView23_2);
     team2_score_accuracy=(TextView)findViewById(R.id.textView24_2);
     team2_judge_fifty=(TextView)findViewById(R.id.textView32_2);
     team2_actual_fifty=(TextView)findViewById(R.id.textView33_2);
     team2_fifty_accuracy=(TextView)findViewById(R.id.textView34_2);
     team2_judge_wicket=(TextView)findViewById(R.id.textView41_2);
     team2_actual_wicket=(TextView)findViewById(R.id.textView42_2);
     team2_wicket_accuracy=(TextView)findViewById(R.id.textView43_2);









}

User user = SharedPrefManager.getInstance(Result_1.this).getUser();
String userEmail= user.getEmail();


private void getResults() {

    loading = ProgressDialog.show(this,"","",false,false);
    String url = Config.Result.toString().trim();


    StringRequest stringRequest = new StringRequest(url, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            loading.dismiss();
            showJSON(response);
        }
    },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    loading.dismiss();
                }
            });

    RequestQueue requestQueue = Volley.newRequestQueue(this);
    requestQueue.add(stringRequest);


}

private void showJSON(String response){
    String Winning="";
    String Rank="";
    String Accuracy="";

    String Team1_Judgement_score="";
    String Team1_Actual_score="";
    String Team1_ScoreAccuracy="";

    String Team1_Judgement_fifty="";
    String Team1_Actual_fifty="";
    String Team1_FiftyAccuracy="";

    String Team1_Judgement_wickets="";
    String Team1_Actual_wickets="";
    String Team1_WicketsAccuracy="";

    String Team2_Judgement_score="";
    String Team2_Actual_score="";
    String Team2_ScoreAccuracy="";

    String Team2_Judgement_fifty="";
    String Team2_Actual_fifty="";
    String Team2_FiftyAccuracy="";

    String Team2_Judgement_wickets="";
    String Team2_Actual_wickets="";
    String Team2_WicketsAccuracy="";



    try {
        JSONObject jobj = new JSONObject(response);
        JSONArray result = jobj.getJSONArray(Config.JSON_ARRAY_Result);

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


                String gmail = result.getJSONObject(i).getString(Config.email);

                if (userEmail.equals(gmail))  // compare user email here
                {

                    Rank = jobj.getString(Config.rank);
                    Accuracy = jobj.getString(Config.accuracy);

                    Team1_Judgement_score = jobj.getString(Config.score1_judge);
                    Team1_Actual_score = jobj.getString(Config.score1_actual);
                    Team1_ScoreAccuracy = jobj.getString(Config.score1_accu);

                    Team1_Judgement_fifty = jobj.getString(Config.fifty1_judge);
                    Team1_Actual_fifty = jobj.getString(Config.fifty1_actual);
                    Team1_FiftyAccuracy = jobj.getString(Config.fifty1_accu);

                    Team1_Judgement_wickets = jobj.getString(Config.wicket1_judge);
                    Team1_Actual_wickets = jobj.getString(Config.wicket1_actual);
                    Team1_WicketsAccuracy = jobj.getString(Config.wicket1_accu);

                    Team2_Judgement_score = jobj.getString(Config.score2_judge);
                    Team2_Actual_score = jobj.getString(Config.score2_actual);
                    Team2_ScoreAccuracy = jobj.getString(Config.score2_accu);

                    Team2_Judgement_fifty = jobj.getString(Config.fifty2_judge);
                    Team2_Actual_fifty = jobj.getString(Config.fifty2_actual);
                    Team2_FiftyAccuracy = jobj.getString(Config.fifty2_accu);

                    Team2_Judgement_wickets = jobj.getString(Config.wicket2_judge);
                    Team2_Actual_wickets = jobj.getString(Config.wicket2_actual);
                    Team2_WicketsAccuracy = jobj.getString(Config.wicket2_accu);

                }



        }

            rank.setText(Rank);
            accuracy.setText(Accuracy);
            team1_judge_score.setText(Team1_Judgement_score);
            team1_actual_score.setText(Team1_Actual_score);
            team1_score_accuracy.setText(Team1_ScoreAccuracy);
            team1_judge_fifty.setText(Team1_Judgement_fifty);
            team1_actual_fifty.setText(Team1_Actual_fifty);
            team1_fifty_accuracy.setText(Team1_FiftyAccuracy);
            team1_judge_wicket.setText(Team1_Judgement_wickets);
            team1_actual_wicket.setText(Team1_Actual_wickets);
            team1_wicket_accuracy.setText(Team1_WicketsAccuracy);

            team2_judge_score.setText(Team2_Judgement_score);
            team2_actual_score.setText(Team2_Actual_score);
            team2_score_accuracy.setText(Team2_ScoreAccuracy);
            team2_judge_fifty.setText(Team2_Judgement_fifty);
            team2_actual_fifty.setText(Team2_Actual_fifty);
            team2_fifty_accuracy.setText(Team2_FiftyAccuracy);
            team2_judge_wicket.setText(Team2_Judgement_wickets);
            team2_actual_wicket.setText(Team2_Actual_wickets);
            team2_wicket_accuracy.setText(Team2_WicketsAccuracy);





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

}



   }

config.java

        public class Config {


public static final String Result = 
"http://thehostels.in/judgement_files/myresult.php";
public static final String email = "email";
public static final String name1 = "name";
public static final String rank = "rank";
public static final String accuracy= "accuracy";
public static final String match_id = "match_id";
public static final String rs1 = "rs";
public static final String score1_judge = "score1_judge";
public static final String score1_actual= "score1_actual";
public static final String score1_accu = "score1_accu";
public static final String wicket1_judge = "wicket1_judge";
public static final String wicket1_actual = "wicket1_actual";
public static final String wicket1_accu= "wicket1_accu";
public static final String fifty1_judge = "fifty1_judge";
public static final String fifty1_actual = "fifty1_actual";
public static final String fifty1_accu = "fifty1_accu";
public static final String score2_judge = "score2_judge";
public static final String score2_actual= "score2_actual";
public static final String score2_accu= "score2_accu";
public static final String wicket2_judge = "wicket2_judge";
public static final String wicket2_actual = "wicket2_actual";
public static final String wicket2_accu= "wicket2_accu";
public static final String fifty2_judge = "fifty2_judge";
public static final String fifty2_actual = "fifty2_actual";
public static final String fifty2_accu = "fifty2_accu";
public static final String JSON_ARRAY_Result= "list";


           }

我的json是这样的

     {"list":[{"email":"vips@gmail.com","name":"Vipul 
       S","rank":"1","accuracy":"80"},
     {"email":"karan@gmail.com","name":"karan 
       S","rank":"3","accuracy":"30"},
      {"email":"vikas@gmail.com","name":"Vikas 
       S","rank":"2","accuracy":"83"}]}

我的xml文件有点大,所以我没有在这里发布,但是我已经检查了ID并且它都没问题。
所以问题是我得到了所有数据,但我只想根据电子邮件一次发布一行,但现在TextView不会发布数据。

1 个答案:

答案 0 :(得分:0)

我这样做了一切都保持不变只是改变

            JSONObject jobj = new JSONObject(response);
    JSONArray result = jobj.getJSONArray(Config.JSON_ARRAY_Result);

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


            String gmail = result.getJSONObject(i).getString(Config.email); 

              JSONObject jobj = new JSONObject(response);
             JSONArray result = jobj.getJSONArray(Config.JSON_ARRAY_Result);

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

                JSONObject Data = result.getJSONObject(i);
                String gmail = 
            result.getJSONObject(i).getString(Config.email);

这是我在函数getResults()的try块中缺少的行,因为没有传递任何引用JSONObject Data = result.getJSONObject(i);

所以用它来帮助你自己......谢谢