0

I have been using VFP 8.0 for quite sometime and one of the most thing I enjoyed about it is the macro function;

name = "Paul James"
age = 25
result = My name is &name, I am &age years old.

I could also do,

dimension x[5];
x[0] = "box"
x[1] = "area"
text.&x[0]..text = "textbox" ---> textbox.text="textbox"
text.&x[1]..text = "textarea" ---> textarea.text="textarea"

That's with the FoxPro thing, I seem to have grown attached to it and am somewhat inclined wishing such exist with OOs Languages like Java (or it really does, im just missing some extra research?), anyway, I wanted to have something like that here's my problem;

I have a JSON Array, which I get all names of the response and store it in a temporary array by using the "names()" method provided in the android code factory.

Purposely, I want to create an array for each name in the temporary array that was created from the method;

To illustrate;

JSONArray response = 
[{"name":"a","middlename":"aa","surname":"aaa"},{"name":"b","middlename":"bb","surname":"bbb"},{"name":"c","middlename":"cc","surname":"ccc"}]

temp[] = [{name,middlename,surname}];

Desired Output:

String[] name = new String[response.firstobject.length];
String[] middlename = new String[response.firstobject.length];
String[] surname =  new String[response.firstobject.length];

Here's my actual code; The JSON Parser

    @SuppressWarnings("null")
public ArrayList<String> parseJson(JSONArray ja) throws JSONException{
    ArrayList<String> listItems = new ArrayList<String>();
    String[] temp = null;       
    //Get all the fields first
    for (int i=0; i<=0; ++i){
        JSONObject jo = ja.getJSONObject(i);
        if(jo.length()>0){
            temp = new String[jo.names().length()];
            for(int x=0; x<jo.names().length(); ++x){
                temp[x] = jo.names().getString(x);
            }
        }
    }
     }

So I'm kinda stuck in the desired output, is this possible in the first place? Why I'm doing this, is that because I wanted to create a generic JSON response method; So that I don't have to remember all the names of the response just to use them. Im looking for a java/android solution (most likely the one that works with android).

Thanks in Advance!

mirageservo
  • 2,387
  • 4
  • 22
  • 31

2 Answers2

2

I wouldn't necessarily try to replicate what you can do in Visual FoxPro since it's usually a good idea in that language to avoid macro substitution unless you absolutely have to use it, and you can't use a name expression instead.

Here is an example of a name expression:

STORE 'city' TO cVarCity
REPLACE (cVarCity) WITH 'Paris'

This is much faster especially in loops.

On the Java side you're probably looking at using the Reflection API.

Alan B
  • 4,086
  • 24
  • 33
  • "you can't use name substition instead", sounds confusing, please correct my understanding, I can't or I can use name substitution? – mirageservo May 09 '12 at 09:37
  • but again, the name expression doesn't fulfill my objectives, I'm currently taking an in depth look about Reflection which you've pointed out, however, it seems advance for my current knowledge with regards to java+android, Im losing hope and might end up with the laziness stuff "HardCoding", unfortunately, this would ruin any OO concepts when it comes to re-usability and flexibility. – mirageservo May 09 '12 at 10:17
  • i may also add, that I may be swaying away from the correct way of doing things, i might be taking a double edged axe whose ends cuts thru between readability and performance or non-standard and standard way of doing things... – mirageservo May 09 '12 at 10:20
0

I also work with vfp and I have some routines. Perhaps these functions serve you STRTRAN, CHRTRAN:

//--------- ejemplos :
// STRTRAN("Hola * mundo","*", "//") ==> "Hola // mundo"
public String STRTRAN(String cExpression, String cFindString, String cReplacement){        
    return cExpression.replace(cFindString, cReplacement);
}

//------------------ ejemplos:
// miToolkit.CHRTRAN("ABCDEF", "ACE", "XYZ"); // muestra XBYDZF. ok
// miToolkit.CHRTRAN("ABCDEF", "ACE", "XYZQRST");  // muestra XBYDZF. ok
// miToolkit.CHRTRAN("ABCD", "ABC", "YZ"); // muestra YZCD.  No es como fox
public String CHRTRAN(String cString, String cFindChars, String cNewChars){
    String cResult = cString;       
    char[] aFindChars; 
    char[] aNewChars;  
    int nLength = cFindChars.length();

    aFindChars = cFindChars.toCharArray();
    aNewChars  = cNewChars.toCharArray();
    if(cNewChars.length() < nLength){
        nLength = cNewChars.length() ;
    }
    for(int i=0; i < nLength; i++){
        cResult = cResult.replace( aFindChars[i], aNewChars[i] );
    }
    return cResult;
}

Saludos, César Gómez, Lima-Perú

cesin
  • 99
  • 5