如何通过代码删除 json url 中的单词?

how to remove a word inside json url by the code?

我有一个 json url: http://wach.ma/mobile/item.php?id=15236

我想解析内容并在 recyclerview 中显示。

一切都很好,但是当我 运行 它发生这个错误:

java.lang.String 类型的值数组无法转换为 JSONObject

在我的 json 文件中有一个词我想通过代码将其删除(因为我无法编辑资源)

Array <------------//i want to remove this word
{"articles" : [{"name":"fiat uno ",
"description":"fiat uno 
model2002",
"price":"32000dh",
"seen":"5",
"username":"Kech",
"picture":"http:\/\/www.wach.ma\/files\/pictures\/1508420901.png",
"city":"Marrakech",
"phone":"0666353083"}]}

我的 activity 我想做的地方:

public class Details extends AppCompatActivity {
private static final String URL_DATA = "http://wach.ma/mobile/item.php?
id=15236";

private RecyclerView recyclerView2;
private RecyclerView.Adapter adapter;
private List<ListDetail> listDetails;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_details);
    recyclerView2= (RecyclerView) findViewById(R.id.recyclerview2);
    recyclerView2.setHasFixedSize(true);
    recyclerView2.setLayoutManager(new LinearLayoutManager(this));
    listDetails=new ArrayList<>();
    loadRecyclerViewData();
}
private void loadRecyclerViewData(){
    final ProgressDialog progressDialog = new ProgressDialog(this);
    progressDialog.setMessage("Chargement...");
    progressDialog.show();
    StringRequest stringRequest=new StringRequest(Request.Method.GET, 
URL_DATA, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            progressDialog.dismiss();
            try {
                JSONObject jsonObject2=new JSONObject(response);
                JSONArray array=jsonObject2.getJSONArray("articles");
                for(int i = 0; i<array.length();i++) {
                    JSONObject o = array.getJSONObject(i);
                    ListDetail item = new ListDetail(
                            o.getString("picture"),
                            o.getString("name"),
                            o.getString("city"),
                            o.getString("username"),
                            o.getString("price"),
                            o.getString("phone"),
                            o.getString("description")
                    );
                    listDetails.add(item);
                }
                adapter = new DetailAdapter(listDetails, 
getApplicationContext());
                recyclerView2.setAdapter(adapter);

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

        }
    },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Toast.makeText(getApplicationContext(), 
error.getMessage(), Toast.LENGTH_LONG).show();
                }
            });
    RequestQueue requestQueue= Volley.newRequestQueue(this);
    requestQueue.add(stringRequest);
}
}

对不起我的英语不好。 提前致谢

您可以使用字符串替换方法 as 并替换单词数组并将其替换为空 String 并因此使用,检查您的方法逻辑是否类似于此代码您还有替代字符串方法,如“ replace()substring(),或查找 "{" 的索引,但总是先 trim()(删除 String 开头和结尾的空格):

try {
            String new_string=response.trim().substring(5);
            JSONObject jsonObjectRoot=new JSONObject(new_string);
            JSONArray jsonArrayArticles=jsonObjectRoot.getJSONArray("articles");
            for(int i = 0; i<array.length();i++) {
                JSONObject o = array.getJSONObject(i);
                ListDetail item = new ListDetail(  
       ....// Your more code here

您不能从您的代码中删除无效的 json 变量。尝试以有效格式

解析 json

试试这个用法 substring()

String newJSON=response.substring(5);
JSONObject jsonObject2=new JSONObject(newJSON);
JSONArray array=jsonObject2.getJSONArray("articles");

问题是由于 json 字符串无效。

始终截断 json 字符串的大括号前后的空格和其他文本...

这样你就可以做到。

 String new_string = response.trim().substring(response.indexOf('{'), 
 response.lastIndexOf('}'));