Android:解析JSONArray

时间:2016-04-20 04:56:00

标签: android arrays json parsing drupal

我需要帮助才能在我的Android应用中解析这个JSONArray。我对JSONObjects和JSONArrays有点困惑:

[
{
    "nid": [
        {
            "value": "3"
        }
    ],

    "uid": [
        {
            "target_id": "1",
            "url": "/user/1"
        }
    ],

    "field_image": [
        {
            "target_id": "2",
            "alt": "alternate 1",
            "title": "",
            "width": "640",
            "height": "640",
            "url": "http://url"
        },
        {
            "target_id": "3",
            "alt": "alternate 2",
            "title": "",
            "width": "640",
            "height": "640",
            "url": "http://url"
        }
    ]
}]

以下是我开始迭代的原因:

public void onResponse(JSONArray response) {
try {
    jsonResponse = "";
    for (int i = 0; i < response.length(); i++) {       
    ...

6 个答案:

答案 0 :(得分:1)

以下是解析数据的代码,

#include <iostream>
#include <string>
#include <queue>
#include <utility>

typedef struct node{
    int level;
    int profit;
    int weight;
    int bound;
} node;

struct node_cmp{
    bool operator()(const node& a, const node& b) const{
        return a.bound < b.bound;
    }
};

int KWF2(int i, int weight, int profit, int *w, int *p, int C, int n){
    int weight1 = weight;
    int bound = profit;
    int j;
    float x[n+1];
    for(j = i; j <= n; j++){
        x[j] = 0;
    }

    while(weight1 < C && (i <= n)){

        if(weight1 + w[i] <= C){
            x[i] = 1;
            weight1 += w[i];
            bound += p[i];
        }
        else{
            x[i] = ((float)C-(float)weight1)/(float)w[i];
            weight1 = C;
            bound = bound + p[i] * x[i];
        }
        i++;
    }
    return bound;
}

void knapsack(int *w, int *p, int C, int maxprofit, int n){
    int maxp = maxprofit;
    std::priority_queue<node,std::vector<node>,node_cmp> PQ;
    node u,v;

    v.level = 0;
    v.profit = 0;
    v.weight = 0;
    v.bound = KWF2(v.level+1,v.weight,v.profit,w,p,C,n);
    PQ.push(v);

    while(!PQ.empty()){
        v = PQ.top();
        PQ.pop();
        if(v.bound > maxp){
            u.level = v.level + 1;
            //yes child
            u.weight = v.weight + w[u.level];
            u.profit = v.profit + p[u.level];

            if((u.weight <= C) && (u.profit > maxp)){
                maxp = u.profit;
            }

            if(KWF2(u.level+1,u.weight,u.profit,w,p,C,n) > maxp){
                PQ.push(u);
            }

            //no child
            u.weight = v.weight;
            u.profit = v.profit;
            u.bound = KWF2(u.level+1,u.weight,u.profit,w,p,C,n);
            if(u.bound > maxp){
                PQ.push(u);
            }
        }
    }
    printf("%d\n",maxp);
}


int main(int argc, char **argv){

    int n,C;
    FILE *in = fopen(argv[1],"r");
    fscanf(in,"%d,%d",&n,&C);

    int w[n+1];
    int p[n+1];
    float ratio[n+1];

    for(int i = 0; i < n; i++){
        fscanf(in,"%d,%d",&w[i+1],&p[i+1]);
        ratio[i+1] = (float)p[i+1]/(float)w[i+1];
    }

    int temp_w,temp_p;
    float temp_r;

    for(int i = 1; i <= n; i++){
        for(int j = i + 1; j <= n; j++){
            if(ratio[i] < ratio[j]){
                temp_w = w[i];
                temp_p = p[i];
                temp_r = ratio[i];

                w[i] = w[j];
                p[i] = p[j];
                ratio[i] = ratio[j];

                w[j] = temp_w;
                p[j] = temp_p;
                ratio[j] = ratio[i];
            }
        }
    }

    int maxprofit = 0;
    knapsack(w,p,C,maxprofit,n);

    fclose(in);
    return 0;
}

答案 1 :(得分:1)

尝试像这样解析。

在此代码中,jsonArray是您在JSON中拥有的父数组。

for(int i=0;i<jsonArray.length();i++)
{
    try {
             JSONObject object=jsonArray.getJSONObject(i);
             JSONArray imageArray=object.getJSONArray("field_image");
             for(int j=0;j<imageArray.length();j++)
             {
                 JSONObject imageObject=imageArray.getJSONObject(j);
                 String targetId=imageObject.getString("target_id");
             }
        } 
    catch (JSONException e) {
                e.printStackTrace();
            }
}

答案 2 :(得分:1)

for (int i = 0; i < response.length(); i++) { 

 JSONObject tobject = response.getJSONObject(i);



  JSONArray nid = tobject.getJSONArray("nid");
  JSONArray uid= tobject.getJSONArray("uid");
  JSONArray field_image= tobject.getJSONArray("field_image");

  //similarly you can loop inner jsonarrays

}

答案 3 :(得分:1)

根据您使用代码:

JSONArray array = null;
    try {
        array = new JSONArray(url); // your web url 
        JSONObject object = array.getJSONObject(0);
        JSONArray array1 = object.getJSONArray("nid");
        JSONObject object1 = array1.getJSONObject(0);
        String value = object1.getString("value");

        JSONArray array2 = object.getJSONArray("uid");
        JSONObject object2 = array2.getJSONObject(0);
        String target = object2.getString("target_id");
        String url = object2.getString("url");

        JSONArray array3 = object.getJSONArray("field_image");
        JSONObject object3 = array3.getJSONObject(0);
        String alt = object3.getString("alt");

        Toast.makeText(Testing.this,value+"\n"+target+"\n"+url+"\n"+alt,Toast.LENGTH_LONG).show();

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

答案 4 :(得分:1)

现在:)如果你必须先解析一些东西,找一些库:

http://www.java2s.com/Code/Jar/g/Downloadgson222jar.htm

下载gson.jar,然后创建模仿所需json的java类:

class C1{
   private String value;
}

class C2{
   private String target_id;
   private String url;
}

class C3{
   private String target_id;
   private String alt;
   private String title;
   private String width;
   private String height;
   private String url;
}

class c4{
   private List<C1> nid;
   private List<C2> uid;
   private List<C3> field_image;
}

由于您从C4接收数组,因此您将解析它:

public void onResponse(JSONArray response){
    String value = response.toString();
    GsonBuilder gb = new GsonBuilder();
    Type arrayType = new TypeToken<List<C4>>() {}.getType();
    List<C4> data = gb.create().fromJson(value, arrayType);
}

因此,只需3行代码,就可以将整个json序列化为可在代码中使用的java对象。

答案 5 :(得分:0)

尝试

public void onResponse(JSONArray response) {
    try {
        if (response != null) {

            for (int i = 0; i < response.length(); i++) {

                JSONObject jsonObject = resultsArray.getAsJsonObject(i);

                //get nid array
                JSONArray nidJSONArray = jsonObject.getJSONArray("nid");

                //get uid array
                JSONArray uidJSONArray = jsonObject.getJSONArray("uid");

                //get field_image array
                JSONArray fieldImageJSONArray = jsonObject.getJSONArray("field_image");

                //parse nid array
                if (nidJSONArray != null) {
                    for (int i = 0; i < nidJSONArray.length(); i++) {
                        JSONObject jsonObject = nidJSONArray.getAsJsonObject(i);
                        String value = jsonObject.getString("value");
                    }
                }


                //parse uid array
                if (uidJSONArray != null) {
                    for (int i = 0; i < uidJSONArray.length(); i++) {
                        JSONObject jsonObject = uidJSONArray.getAsJsonObject(i);
                        String targetId = jsonObject.getString("target_id");
                        String url = jsonObject.getString("url");
                    }
                }


                //parse field_image array
                if (fieldImageJSONArray != null) {
                    for (int i = 0; i < fieldImageJSONArray.length(); i++) {
                        JSONObject jsonObject = fieldImageJSONArray.getAsJsonObject(i);
                        String targetId = jsonObject.getString("target_id");
                        String alt = jsonObject.getString("alt");
                        String title = jsonObject.getString("title");
                        String width = jsonObject.getString("width");
                        String height = jsonObject.getString("height");
                        String url = jsonObject.getString("url");
                    }
                }
            }
        }

    } catch(Exception e) {
        Log.e("Error", e.getMessage());
    }

}