Android的API JSON解析 [英] Android JSON API parsing

查看:179
本文介绍了Android的API JSON解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我曾尝试使用JSON解析几个教程,但我似乎无法使他们的工作。请任何人都好心帮,指导我正确的方向?

我现在已经做了一些改变,以我的previous类与一对夫妇乐于助人的人的帮助,现在我越来越少的错误。请帮助。

 包com.somcollection;进口android.app.Activity;
进口android.os.Bundle;进口org.json.JSONArray;
进口org.json.JSONObject;
进口android.app.Activity;
进口android.os.Bundle;公共类JsonParser延伸活动{
    私人的JSONObject jObject;
    私人字符串的jstring = \"{\\\"searchTerm\\\":\\\"N7\\\",\\\"searchableLocations\\\":[{\\\"identifier\\\":\\\"OUT$c$c^1685\\\",\\\"name\\\":\\\"N7\\\"}],\\\"createDate\\\":1321834118923,\\\"result\\\":\\\"SUCCESS\\\"}\";    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);
        尝试{
            解析();
        }赶上(例外五){
            e.printStackTrace();
        }
    }    私人无效解析()抛出异常{
        jObject =新的JSONObject(的jstring);
        JSONObject的jObject =新的JSONObject(的jstring);
        串menuObject = jObject.getString(SEARCHTERM);
        JSONArray menuitemArray = jObject.getJSONArray(searchableLocations);
        的for(int i = 0; I< menuitemArray.length();我++){
            JSONObject的jsonObj = menuitemArray.getJSONObject(I)
            字符串attributeId = jsonObj.getString(标识);
            的System.out.println(attributeId);
            字符串的AttributeValue = jsonObj.getString(名称);
            的System.out.println(的AttributeValue);
        }
        串CREATEDATE = jObject.getString(jObject);
        字符串结果= jObject.getString(结果);
    }
}


解决方案

JSON对象可能​​包含了一些关键值对。在的jstring张贴在你的问题,如搜索关键词,searchableLocations等字符串的字符串键。该值也能像字符串,整数,Bollean,JSonArray等什么对应的关键searchableLocations值是JsonArray(由[]表示)。

 的JSONObject jObject =新的JSONObject(的jstring);
    串menuObject = jObject.getString(SEARCHTERM);
    JSONArray menuitemArray = jObject.getJSONArray(searchableLocations);    的for(int i = 0; I< menuitemArray.length();我++){
        JSONObject的jsonObj = menuitemArray.getJSONObject(I)
        字符串attributeId = jsonObj.getString(标识);
        的System.out.println(attributeId);        字符串的AttributeValue = jsonObj.getString(名称);
        的System.out.println(的AttributeValue);
    }    串CREATEDATE = jObject.getString(jObject);
    字符串结果= jObject.getString(结果);

I have tried several tutorials using JSON parsing but I can't seem to make them work. Please can anyone kindly help and direct me to the right direction?

I have now made some changes to my previous class with the help of a couple of helpful people, and I'm now getting less errors. Please help.

package com.somcollection;

import android.app.Activity;
import android.os.Bundle;

import org.json.JSONArray;
import org.json.JSONObject;
import android.app.Activity;
import android.os.Bundle;

public class JsonParser extends Activity {
    private JSONObject jObject;
    private String jString = "{\"searchTerm\":\"N7\",\"searchableLocations\":[{\"identifier\":\"OUTCODE^1685\",\"name\":\"N7\"}],\"createDate\":1321834118923,\"result\":\"SUCCESS\"}";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        try {
            parse();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void parse() throws Exception {
        jObject = new JSONObject(jString);
        JSONObject jObject = new JSONObject(jString);
        String menuObject = jObject.getString("searchTerm");
        JSONArray menuitemArray = jObject.getJSONArray("searchableLocations");
        for (int i = 0; i < menuitemArray.length(); i++) {
            JSONObject jsonObj = menuitemArray.getJSONObject(i);
            String attributeId = jsonObj.getString("identifier");
            System.out.println(attributeId);
            String attributeValue = jsonObj.getString("name");
            System.out.println(attributeValue);
        }
        String createDate = jObject.getString("jObject");
        String result = jObject.getString("result");
    }
}

解决方案

Json object may contain a number of key value pairs. In the jString posted in your question, strings like "searchTerm", "searchableLocations", etc are the String keys. The values could be anything like String, Int, Bollean, JSonArray, etc. Corresponding to the key "searchableLocations" the value is a JsonArray (denoted by [ ]).

    JSONObject jObject = new JSONObject(jString);    
    String menuObject = jObject.getString("searchTerm");     
    JSONArray menuitemArray = jObject.getJSONArray("searchableLocations");   

    for (int i = 0; i < menuitemArray.length(); i++) {
        JSONObject jsonObj = menuitemArray.getJSONObject(i);     
        String attributeId = jsonObj.getString("identifier");     
        System.out.println(attributeId);     

        String attributeValue = jsonObj.getString("name");    
        System.out.println(attributeValue); 
    }

    String createDate = jObject.getString("jObject");
    String result = jObject.getString("result");

这篇关于Android的API JSON解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆