如何为android创建通用JSON解析器
可以创建更通用的JSON解析器吗 在代码中没有提到任何父节点或子节点标记,我需要解析JSON文件并在Android活动中显示它 问候,,如何为android创建通用JSON解析器,android,json,parsing,Android,Json,Parsing,可以创建更通用的JSON解析器吗 在代码中没有提到任何父节点或子节点标记,我需要解析JSON文件并在Android活动中显示它 问候,, Deepak Krishnan您可以定义自己的json解析器类- public class JSONParser { static InputStream is = null; static JSONObject jObj = null; static JSONArray jsonArray = null; static St
Deepak Krishnan您可以定义自己的json解析器类-
public class JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static JSONArray jsonArray = null;
static String json = "";
// constructor
public JSONParser() {
}
public JSONArray getJSONFromUrl(String url, List<NameValuePair> params) {
System.out.println("url:: "+url );
System.out.println("params:: "+ params +" " +params.get(0) );
// Making HTTP request
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(params));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
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);
}
is.close();
json = sb.toString();
//Log.e("JSON::: ", json);
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
// try parse the string to a JSON object
try {
if(!json.equals("null")){
jsonArray = new JSONArray(json);
Log.d("jsonArray:: ", jsonArray+"");
}else{
jsonArray = null;
}
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON String
return jsonArray;
}
}
公共类JSONParser{
静态InputStream为空;
静态JSONObject jObj=null;
静态JSONArray JSONArray=null;
静态字符串json=“”;
//建造师
公共JSONParser(){
}
公共JSONArray getJSONFromUrl(字符串url,列表参数){
System.out.println(“url::”+url);
System.out.println(“参数::”+params+“”+params.get(0));
//发出HTTP请求
试一试{
//defaultHttpClient
DefaultHttpClient httpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(url);
setEntity(新的UrlEncodedFormEntity(参数));
HttpResponse HttpResponse=httpClient.execute(httpPost);
HttpEntity HttpEntity=httpResponse.getEntity();
is=httpEntity.getContent();
}捕获(不支持的编码异常e){
e、 printStackTrace();
}捕获(客户端协议例外e){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
试一试{
BufferedReader reader=新的BufferedReader(新的InputStreamReader(
is,“iso-8859-1”),8);
StringBuilder sb=新的StringBuilder();
字符串行=null;
而((line=reader.readLine())!=null){
某人附加(行);
}
is.close();
json=sb.toString();
//Log.e(“JSON::”,JSON);
}捕获(例外e){
Log.e(“缓冲区错误”,“错误转换结果”+e.toString());
}
//尝试将字符串解析为JSON对象
试一试{
如果(!json.equals(“null”)){
jsonArray=新的jsonArray(json);
d(“jsonArray::”,jsonArray+“”);
}否则{
jsonArray=null;
}
}捕获(JSONException e){
Log.e(“JSON解析器”,“错误解析数据”+e.toString());
}
//返回JSON字符串
返回jsonArray;
}
}
你的职业是什么-
public class UserFuctions {
private JSONParser jsonParser;
// constructor
public UserFuctions(){
jsonParser = new JSONParser();
}
private static String HOST_URL = "http://100.43.0.21/pharmacy";
public static final String FS = File.separator;
private static String genericListByNameSearchURL = HOST_URL+FS +"getGenericByName.php";
/**
*
* function make Login Request
* @param email
* @param password
* */
public JSONArray getGenericByName(String genName){
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("genName", genName));
JSONArray json = jsonParser.getJSONFromUrl(getGenericByName, params);
return json;
}
}
公共类用户功能{
私有JSONParser JSONParser;
//建造师
公共用户功能(){
jsonParser=新的jsonParser();
}
私有静态字符串主机\u URL=”http://100.43.0.21/pharmacy";
公共静态最终字符串FS=File.separator;
私有静态字符串genericListByNameSearchURL=HOST_URL+FS+“getGenericByName.php”;
/**
*
*函数发出登录请求
*@param电子邮件
*@param密码
* */
公共JSONArray getGenericByName(字符串genName){
//建筑参数
List params=new ArrayList();
添加(新的BasicNameValuePair(“genName”,genName));
JSONArray json=jsonParser.getJSONFromUrl(getGenericByName,参数);
返回json;
}
}
您可以定义自己的json解析器类-
public class JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static JSONArray jsonArray = null;
static String json = "";
// constructor
public JSONParser() {
}
public JSONArray getJSONFromUrl(String url, List<NameValuePair> params) {
System.out.println("url:: "+url );
System.out.println("params:: "+ params +" " +params.get(0) );
// Making HTTP request
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(params));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
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);
}
is.close();
json = sb.toString();
//Log.e("JSON::: ", json);
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
// try parse the string to a JSON object
try {
if(!json.equals("null")){
jsonArray = new JSONArray(json);
Log.d("jsonArray:: ", jsonArray+"");
}else{
jsonArray = null;
}
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON String
return jsonArray;
}
}
公共类JSONParser{
静态InputStream为空;
静态JSONObject jObj=null;
静态JSONArray JSONArray=null;
静态字符串json=“”;
//建造师
公共JSONParser(){
}
公共JSONArray getJSONFromUrl(字符串url,列表参数){
System.out.println(“url::”+url);
System.out.println(“参数::”+params+“”+params.get(0));
//发出HTTP请求
试一试{
//defaultHttpClient
DefaultHttpClient httpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(url);
setEntity(新的UrlEncodedFormEntity(参数));
HttpResponse HttpResponse=httpClient.execute(httpPost);
HttpEntity HttpEntity=httpResponse.getEntity();
is=httpEntity.getContent();
}捕获(不支持的编码异常e){
e、 printStackTrace();
}捕获(客户端协议例外e){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
试一试{
BufferedReader reader=新的BufferedReader(新的InputStreamReader(
is,“iso-8859-1”),8);
StringBuilder sb=新的StringBuilder();
字符串行=null;
而((line=reader.readLine())!=null){
某人附加(行);
}
is.close();
json=sb.toString();
//Log.e(“JSON::”,JSON);
}捕获(例外e){
Log.e(“缓冲区错误”,“错误转换结果”+e.toString());
}
//尝试将字符串解析为JSON对象
试一试{
如果(!json.equals(“null”)){
jsonArray=新的jsonArray(json);
d(“jsonArray::”,jsonArray+“”);
}否则{
jsonArray=null;
}
}捕获(JSONException e){
Log.e(“JSON解析器”,“错误解析数据”+e.toString());
}
//返回JSON字符串
返回jsonArray;
}
}
你的职业是什么-
public class UserFuctions {
private JSONParser jsonParser;
// constructor
public UserFuctions(){
jsonParser = new JSONParser();
}
private static String HOST_URL = "http://100.43.0.21/pharmacy";
public static final String FS = File.separator;
private static String genericListByNameSearchURL = HOST_URL+FS +"getGenericByName.php";
/**
*
* function make Login Request
* @param email
* @param password
* */
public JSONArray getGenericByName(String genName){
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("genName", genName));
JSONArray json = jsonParser.getJSONFromUrl(getGenericByName, params);
return json;
}
}
公共类用户功能{
私有JSONParser JSONParser;
//建造师
公共用户功能(){
jsonParser=新的jsonParser();
}
私有静态字符串主机