JSON: Get node value, : JSON « Development « Android






JSON: Get node value,

   

//package com.omar.labs.tamtamy.JSON;

import java.net.URLEncoder;
import java.util.ArrayList;

public class JSONUtility {

  public static String getNodeValue(String json, String propName){
    String tmp = null;
    int match = json.indexOf(propName+":");
    if(match!=-1){
    int start = match + (propName+":").length();
    int stop = json.indexOf(",", start);    
    try{
      if(stop>0 && stop-start>1 && json.charAt(start)=='"')        
        tmp = json.substring(start+1, stop-1);
      else if(stop>0)
        tmp = json.substring(start, stop);  
      else
        tmp = json.substring(start+1);  
    }catch (Exception ex){

    }    
    if(tmp.indexOf("\"}")!=-1)
      tmp = tmp.substring(0, tmp.length()-2);
    }
    return tmp; 
  }
  
  public static ArrayList getNodesValue(String json){
    
    if(json.startsWith("\""))
      json = json.substring(1);
    if(json.endsWith("\"")) 
      json = json.substring(0, json.length()-1); 
    
    if(json.startsWith("["))
      json = json.substring(1);
    if(json.endsWith("]")) 
      json = json.substring(0, json.length()-1); 
      
    String[] tmp = json.split(",");
    ArrayList<String> tmpArr = new ArrayList<String>();
    for(int t=0; t<tmp.length; t++){
      String element = tmp[t];
      if(element.startsWith("\""))
          element = element.substring(1);
      if(element.endsWith("\"")) 
        element = element.substring(0, element.length()-1);        
      tmpArr.add(element);
    }
        
    return tmpArr;
  }
  
  /*
  public static String getNodeValueWithList(String json, String propName){    
    String tmp = null;    
    int start = json.indexOf(propName+":") + (propName+":").length();
    int stop = json.indexOf("]", start);    
    try{
      if(stop>0)
        tmp = json.substring(start, stop);
      else
      tmp = json.substring(start);  
    }catch (Exception ex){
        
    }    
    if(tmp.indexOf("\"")!=-1)
      tmp = tmp.substring(1, tmp.length()-1);    
    return tmp; 
  }
  */
  
  public static ArrayList<String> getNodeValueWithList(String json, String propName){    
    ArrayList<String> tmpA = new ArrayList<String>();
    String tmp = null;    
    int start = json.indexOf(propName+":") + (propName+":").length();
    int stop = json.indexOf("]", start);
    int stop1 = json.lastIndexOf("]");
    try{
      if (stop>0 && stop == stop1){
      tmp = json.substring(start+1, stop);
      }else if(stop1>0){
      tmp = json.substring(start+1, stop1);   
      }else{  
      tmp = json.substring(start, json.length()-1);
      }  
    }catch (Exception ex){
        ex.printStackTrace();
    }    
    //if(tmp.indexOf("\"")!=-1)
    //  tmp = tmp.substring(1, tmp.length());
    
    String[] items = null;
    if(tmp.indexOf("},{")==-1 && !propName.equals("\"list\""))
      items = tmp.split(",");
    else
      items = tmp.split("\\},\\{");
    for(int i=0; i<items.length; i++){
      tmpA.add(items[i]);
    }        
    return tmpA; 
  }  
}

   
    
    
  








Related examples in the same category

1.Parse Json string
2.Upload Image to Flickr with JSON
3.Flickr JSON Location
4.Using JSON
5.Json Client
6.Get List From Json Object
7.Create JSONObject
8.Data type conversion for JSON
9.Object to JSON
10.Wikidictionary
11.Browse through Wiktionary content
12.Utility class supporting the Facebook Object.