如何在ajax调用中获取内部键值和数据?

时间:2017-04-28 05:34:33

标签: javascript jquery ajax

我在jquery

中尝试了以下代码行
        import java.util.ArrayList;
    import java.util.Date;
    public class AmusementPark {
    private ArrayList<Ticket> tickets;
    private ArrayList<Merchandise> merchandise;
    private String name;
    AmusementPark(String name){
        this.name=name;
        this.tickets = new ArrayList<Ticket>();
        this.merchandise= new ArrayList<Merchandise>();
    }
    String getName(){
        return name;



    }
     public ArrayList<String> getTicketDates(){
        ArrayList<String> theDateArray= new ArrayList<>();
        int i;
        String date = Ticket.getDate();    //This is not working.  See Reason Below.

        for (i=0; i<tickets.size(); i++){
            if(tickets.get(i).getPurchased()== false){
                theDateArray.add(date);
            }

        }return theDateArray;
    }
    }

在控制台数据中就像

0 : 宾语 1 : 阵列(4) 0 : 阵列(1) 1 : 阵列(2) 0 : 宾语 物理 : “150.00 / 200” 状态 : “通过” 的 : 宾语 1 : 宾语 科学 : “400.00 / 600” 状态 : “通过” 的 : 宾语 ...........

我希望li中的数据看起来像

每个科目的标记

              <h5>Marks per Subjects</h5>
               <ul id="subjectsMarks">

               </ul>


           $.ajax({
                url: 'index.php?action=fetchStudentIfResultFound',
                type: 'POST',
                dataType: 'JSON',
                ...
                ...

            for(i=0; i<data[1].length; i++)
              {
               $("#subjectsMarks li").html(key[1][i] + ':' + data[1][i]+ "<br/>"+data[1]["Status"]);
              }
              });

请帮帮我!!!

1 个答案:

答案 0 :(得分:1)

从后端修改数据并向数据添加键,如下所示,很容易知道必须循环的地方,不需要知道何时停止

        const string accountSid = "ACXXXXXXXXXXXXXXXXXXXXXXXXX";
        const string authToken = "909dajhdusa78798324ndkjsahd";
        TwilioClient.Init(accountSid, authToken);

        var to = new PhoneNumber("+919000000000");
        var from = new PhoneNumber("+919000000000");
        var call = CallResource.Create(to,
                                       from,
                                       url: new Uri("http://twimlets.com/echo?Twiml=test")); //Not working

        call = CallResource.Create(to,
                                       from,
                                       url: new Uri("http://twimlets.com/echo?Twiml=<Response><Say>testing</Say></Response>"));//Not working


        call = CallResource.Create(to,
                                       from,
                                       url: new Uri("http://twimlets.com/echo?Twiml=%3CResponse%3E%3CSay%3EHi+there.%3C%2FSay%3E%3C%2FResponse%3E"));//Not working


        Console.WriteLine(call.Sid);

使用以下代码尝试循环

var data: {
  student: {
    "_id": {
      "$id": "5715f58299fbad800100003a"
    },
    "student_id": 36,
    "‌​registration_temp_pe‌​rm_no": "453654",
    "rol‌​l_no": "31",
    "admissio‌​n_date": "04/12/2016"‌​,
    "first_name": "Momin‌​",
    "middle_name": "Bas‌​hir",
    "last_name": "Ra‌​ther",
    "dob": "7/9/199‌​5",
    "gender": "Male",
    "‌​blood_group": "",
    "bir‌​th_place": "",
    "nation‌​ality": "",
    "language"‌​: "",
    "religion": "Isla‌​m"...
  },
  marks: {
    subjects: [
      {
        "Physic‌​s": "150.00/200",
        "Sta‌​tus": "Pass"
      },
      {
        "Scien‌​ce": "400.00/600",
        "St‌​atus": "Pass"
      }
    ],
    "Tot‌​al Marks": "550/800",
    "Grade": "Ist",

  }
}

第二版循环

//Without modify json from backend
var allSubjects=data[1][1]; 

//With modify json from backend
var allSubjects=data[1][1]['Sub‌​jects']; 

for(var i in allSubjects){


 var keysOfObject=Object.Keys(allSubjects[i]);

  $("#subjectsMarks li").html(keysOfObject[0] + ':' + allSubjects[i][keysOfObject[0]]+ "<br/>"allSubjects[i]["Status"]);
/* //console.log(allSubjects[i].keysOfObject[0], keysOfObject[0])
 console.log(allSubjects[i]);*/


}