如何从jsonarray获取jsonobject的关键和值?

时间:2017-11-22 07:21:02

标签: android json

我有一个像这样的json:

{
  "FindingMedia": [
    {
      "ScheduleDetailID": 12414,
      "AnswerId": 2,
      "findID": 2,
      "mediaID": 3,
      "Files": "xxx",
      "ExtFiles": "jpg"
    },
    {
      "ScheduleDetailID": 12414,
      "AnswerId": 2,
      "findID": 2,
      "mediaID": 4,
      "Files": "xxxx",
      "ExtFiles": "jpg"
    }
  ]
}

如何获得在android中插入我的sqlite的密钥和值?

我有这样的代码:

JSONObject jsonObject=new JSONObject(parsing);
        for(int i=0;i<jsonObject.length();i++)
        {
            JSONArray jsonArray= jsonObject.getJSONArray("FindingMedia");
            db.addContact(new Contact(jsonArray.toString()));
            Log.d("json", jsonArray.toString());
        }

日志的输出是:

  

[{“MediaID”:3,“ExtFiles”:“jpg”,“ScheduleDetailID”:12414,“Files”:“xxx”,“ExtFiles”:“jpg”}]

但是日志的输出有一个符号[{

如何在没有上述符号的情况下获得输出?

喜欢

  

“MediaID”:3,“ExtFiles”:“jpg”,“ScheduleDetailID”:12414,“Files”:“xxx”,“ExtFiles”:“jpg”

编辑:

联系班级:

public class Contact {
    int _id;
    String _name;

    public Contact(){

    }
    public Contact(int id, String name){
        this._id = id;
        this._name = name;
    }

    public Contact(String name){
        this._name = name;
    }

    public int getID(){
        return this._id;
    }

    // setting id
    public void setID(int id){
        this._id = id;
    }

    // getting name
    public String getName(){
        return this._name;
    }

    // setting name
    public void setName(String name){
        this._name = name;
    }
}

2 个答案:

答案 0 :(得分:1)

JSONObject jsonObject=new JSONObject(parsing);     
JSONArray jsonArray = jsonObject.getJSONArray("FindingMedia");
for (int i = 0; i < jsonArray.length(); i++)
{
    JSONObject e = null;
    try {
        e = jsonArray.getJSONObject(i);
        String strAnswerId= e.getString("AnswerId");
        String strFindID= e.getString("findID");

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

}

答案 1 :(得分:0)

您的JSON解析错误

试试这个

        try {
           JSONObject jsonObject = new JSONObject(parsing);
            JSONArray jsonArray= jsonObject.getJSONArray("FindingMedia");
            for(int i=0;i<jsonArray.length();i++)
            {
                JSONObject object=jsonArray.getJSONObject(i);
                Log.d("AuditScheduleDetailID", object.getString("ScheduleDetailID"));
                Log.d("AnswerId", object.getString("AnswerId"));
                Log.d("findID", object.getString("findID"));
                Log.d("mediaID", object.getString("mediaID"));
                Log.d("Files", object.getString("Files"));
                String data="mediaID: "+object.getString("mediaID")+"ExtFiles: "+object.getString("Files")+"ScheduleDetailID: "+object.getString("ScheduleDetailID"); 
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
相关问题