简体   繁体   English

将Json解析为对象或数组中的值的数组

[英]Parsing Json as an Array of values ont objects or arrays

i have this model of Json : 我有这个Json模型:

{
   "conditions":[
      "Offre non cumulable avec les promotions internes de playland",
      "Le centre ferme le lundi",
      "R\u00e9servation \u00e0 l'avance au 0661977474",
      "Les mineurs doivent remplir l'autorisation parentale sur le site www.playland.ma",
      "Valable sur pr\u00e9sentation d'une carte d'identit\u00e9 avec le bon",
      "Le forfait inclut les \u00e9quipements, les protections, le remplissage gaz et l'assurance",
      "Offre valable 1 mois du 1 Mars au 31 mars 2012"
   ]
}

and i need to parse it and put in in one String but i can't do that because the code that i'm using, parse only the first value in this Array and this is the code: 而且我需要解析它并放入一个String中,但是我不能这样做,因为我正在使用的代码仅解析此Array中的第一个值,这是代码:

JSONArray c = deals.getJSONArray(condition);
            for(int i = 0; i < c.length(); i++){
                 String Precision = c.getString(i);}

You can simply use Gson which can opearte on Objects in java and convert object to json and vice-verse. 您可以简单地使用Gson,它可以在Java中的Objects上显示并将对象转换为json,反之亦然。

Link for Gson is - 格森的链接是-

http://code.google.com/p/google-gson/ http://code.google.com/p/google-gson/

try this, it may help you. 试试这个,可能对您有帮助。

JSONObject jObject = new JSONObject(jsonResponseString);

JSONObject arrObject = jObject.getJSONArray("Tagname");

for (int i = 0, len = arrObject.length(); i < len; i++) 
{
    JSONObject json = arrObject.getJSONObject(i);
    System.out.println(json.getString("value1").toString());
    System.out.println(json.getString("value2").toString());
}

Try this code.It is working fine at my end. 尝试这段代码。在我看来,它工作正常。

public class SampleActivity extends Activity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        String json = "[\"Offrenoncumulableaveclespromotionsinternesdeplayland\",\"Lecentrefermelelundi\",\"Réservationàl'avanceau0661977474\",\"Lesmineursdoiventremplirl'autorisationparentalesurlesitewww.playland.ma\",\"Valablesurprésentationd'unecarted'identitéaveclebon\",\"Leforfaitinclutleséquipements,lesprotections,leremplissagegazetl'assurance\",\"Offrevalable1moisdu1Marsau31mars2012\"]";
        try
        {
            JSONArray array = new JSONArray(json);

            for (int i = 0, len = array.length(); i < len; i++)
            {
                Log.i("info", array.getString(i));
            }

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

    }

}

Hope this help. 希望能有所帮助。

Try this:-Vipul shah was correct. 试试这个:-Vipul Shah是正确的。

package com.ap.test;

import org.json.JSONArray;
import org.json.JSONException;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

public class StestActivity extends Activity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    StringBuffer sb = new StringBuffer();

    String json = "[\"Offrenoncumulableaveclespromotionsinternesdeplayland\",\"Lecentrefermelelundi\",\"Réservationàl'avanceau0661977474\",\"Lesmineursdoiventremplirl'autorisationparentalesurlesitewww.playland.ma\",\"Valablesurprésentationd'unecarted'identitéaveclebon\",\"Leforfaitinclutleséquipements,lesprotections,leremplissagegazetl'assurance\",\"Offrevalable1moisdu1Marsau31mars2012\"]";
    try
    {
        JSONArray array = new JSONArray(json);

        for (int i = 0, len = array.length(); i < len; i++)
        {
            Log.i("info", array.getString(i));

            sb.append(array.getString(i));

        }

        TextView sname = (TextView) findViewById(R.id.id_activity_lat);
        sname.setText(sb.toString());

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

    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM