title: Java Android Studio restTemplate parse JSON
body: <p>I'm getting a JSON from a URL - <strong>WooCommerce API</strong>.</p>
  
  <p>The Problem is I cannot parse / output them in the app.</p>
  
  <p>No errors, but it's showing up no data - just the Labels.</p>
  
  <p>MainActivity.java:</p>
  
  <pre><code>    package at.copy_cat.app.rest;
  
  import android.os.AsyncTask;
  import android.os.Bundle;
  import android.support.v4.app.Fragment;
  import android.support.v7.app.ActionBarActivity;
  import android.util.Log;
  import android.view.LayoutInflater;
  import android.view.Menu;
  import android.view.MenuItem;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.TextView;
  
  import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
  import org.springframework.web.client.RestTemplate;
  
  public class MainActivity extends ActionBarActivity {
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          if (savedInstanceState == null) {
              getSupportFragmentManager().beginTransaction()
                      .add(R.id.container, new PlaceholderFragment())
                      .commit();
          }
      }
  
      @Override
      protected void onStart() {
          super.onStart();
          new HttpRequestTask().execute();
      }
  
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
  
          // Inflate the menu; this adds items to the action bar if it is present.
          getMenuInflater().inflate(R.menu.main, menu);
          return true;
      }
  
      @Override
      public boolean onOptionsItemSelected(MenuItem item) {
          // Handle action bar item clicks here. The action bar will
          // automatically handle clicks on the Home/Up button, so long
          // as you specify a parent activity in AndroidManifest.xml.
          int id = item.getItemId();
          if (id == R.id.action_refresh) {
              new HttpRequestTask().execute();
              return true;
          }
          return super.onOptionsItemSelected(item);
      }
  
      /**
       * A placeholder fragment containing a simple view.
       */
      public static class PlaceholderFragment extends Fragment {
  
          public PlaceholderFragment() {
          }
  
          @Override
          public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                   Bundle savedInstanceState) {
              View rootView = inflater.inflate(R.layout.fragment_main, container, false);
              return rootView;
          }
      }
  
  
      private class HttpRequestTask extends AsyncTask&lt;Void, Void, Greeting&gt; {
          @Override
          protected Greeting doInBackground(Void... params) {
              try {
                  final String url = "http://copy-cat.at/api/index.json";
                  RestTemplate restTemplate = new RestTemplate();
                  restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
  
  
  
                  /**Greeting greeting = restTemplate.getForObject(url, Greeting[].class);**/
                Greeting greeting = restTemplate.getForObject(url, Greeting.class);
  
                /**  Greeting greeting = Arrays.asList(restTemplate.getForObject(url, Greeting[].class));**/
                  RestTemplate rest = new RestTemplate();
  
  
  
                  return greeting;
              } catch (Exception e) {
                  Log.e("MainActivity", e.getMessage(), e);
              }
  
              return null;
          }
  
          @Override
          protected void onPostExecute(Greeting greeting) {
              TextView greetingIdText = (TextView) findViewById(R.id.id_value);
              TextView greetingContentText = (TextView) findViewById(R.id.content_value);
              greetingIdText.setText(greeting.getId());
              greetingContentText.setText(greeting.getContent());
          }
  
      }
  
  }
  </code></pre>
  
  <p>Greeting.java:</p>
  
  <pre><code>package at.copy_cat.app.rest;
  import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
  /**
   * Created by Michael on 17.10.2017.
   */
  
  
  
  @JsonIgnoreProperties(ignoreUnknown=true)
  
  public class Greeting {
  
      private String products;
      public String title;
  
  
      public String getId() {
          return this.products;
      }
  
      public String getContent() {
          return this.title;
  
      }
  
  }
  </code></pre>
  
  <p>With a simple JSON like this:
  <strong>{"id":161,"content":"Hello, World!"}</strong>
  It's working, but not with this generated JSON <a href="http://copy-cat.at/api/api.php" rel="nofollow noreferrer">here</a>.</p>
  
  <p>Yes its a .php file but I use in the Java code the .json with the same output like the PHP script...</p>
  
  <p>The question is: how can I parse the information like products by ID etc.</p>
  
  <p>Many thanks!</p>
  
  <p><strong>EDIT/UPDATE:</strong>
  Now its looks like this</p>
  
  <pre><code>package at.copy_cat.app.rest;
  
  import android.util.Log;
  
  import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
  
  import org.json.JSONArray;
  import org.json.JSONException;
  import org.json.JSONObject;
  
  import java.util.HashMap;
  import java.util.List;
  /**
   * Created by Michael on 17.10.2017.
   */
  
  
  
  @JsonIgnoreProperties(ignoreUnknown=true)
  
  public class Greeting {
  
  
      // getters and setters
          public  List products;
  
          public String getContent(){
              String convertedToString = "" + products;
              /**String[] value_split = convertedToString.split(",");**/
              String[] value_split = convertedToString.split("\\,");
  
  
              try {
  
                  JSONArray json = new JSONArray(products);
  
  
                  for(int i=0;i&lt;json.length();i++){
                      HashMap&lt;String, String&gt; map = new HashMap&lt;String, String&gt;();
                      JSONObject e = json.getJSONObject(i);
  
                     /** Log.v("TEST23243235", "TESTOBJ" + e);**/
                      /**JSONArray people = e.getJSONArray("products");
                      int numOfPeople = e.getInt("images");**/
                      map.put("id",  String.valueOf(i));
                      map.put("productid", "Product ID:" + e.getString("id"));
                      map.put("title", "Title: " +  e.getString("title"));
                      map.put("permalink", "Permalink: " +  e.getString("permalink"));
                      map.put("status", "Status: " +  e.getString("status"));
                      map.put("regular_price", "Regular Price: " +  e.getString("regular_price"));
                      map.put("price", "Price: " +  e.getString("price"));
                    //  map.put("description", "Description: " +  e.getString("description"));
                    //  map.put("categories", "Categories: " +  e.getString("categories"));
                     // map.put("images", "Images: " +  e.getString("images"));
  
  
                      Log.v("TEST23243235", "TESTOBJ" + map);
                      Log.v("TEST23243235", "TESTOBJ" +  json);
                      Log.v("TEST23243235", "TESTOBJI" +  i);
                      return map.toString();
                  }
  
  
  
                  // Extract data from json and store into ArrayList as class objects
  
                      /**JSONObject json_data = jArray.getJSONObject(i);
  
                      String id = jObj.getString("id");
  
                      Log.d("TEST23243235", "TEST" + id);**/
  
  
  
  
  
              } catch (JSONException e) {
          Log.e("TEST23243235", "unexpected JSON exception", e);
          // Do something to recover ... or kill the app.
  
          }
              return "";
  
  
          }
  
          public void setProducts(List products){
  
              this.products = products;
          }
          public class Model {
              private String title;
  
              //other fields
  
              //getters and setters
          }
  
  
  
  
  
      public String getId() {
          return "1";
      }
  
     /** public String getContent() {
       return this.convertedToString;
  
      }**/
  
  }
  </code></pre>
  
  <p>Output in Logcat</p>
  
  <pre><code>TESTOBJ{price=Price: 30.00, regular_price=Regular Price: 0.00, title=Title: Camera DS, status=Status: publish, permalink=Permalink: http://copy-cat.at/produkt/camera-ds-2, id=0, productid=Product ID:590}
  
  TESTOBJI0
  </code></pre>
  
  <p>So i can output now 1 product from the Json but not two or more....
  What can i do to parse all products an not just one?</p>
  
  <p>Yes code is not clean - sorry about that i learning... :)</p>