Java 用引号解析json数据
我制作了一个android应用程序,用于解析URL中的数据。只要返回的json数据中没有引号,应用程序就可以正常工作。当数据有如下标记时,有没有一种方法可以用Java解析json数据:Java 用引号解析json数据,java,android,json,parsing,Java,Android,Json,Parsing,我制作了一个android应用程序,用于解析URL中的数据。只要返回的json数据中没有引号,应用程序就可以正常工作。当数据有如下标记时,有没有一种方法可以用Java解析json数据: { "cate": [ { "title": "this is the "title" like ", "EdId": "445", "date": "Sep 25, 2014" } ] } 以下是我正在使用的代码: public class Guys e
{
"cate":
[
{
"title": "this is the "title" like ",
"EdId": "445",
"date": "Sep 25, 2014"
}
]
}
以下是我正在使用的代码:
public class Guys extends Activity {
ListView list;
TextView title;
TextView EdId;
TextView date;
Button Btngetdata;
//Button nextac;
ArrayList<HashMap<String, String>> oslist = new ArrayList<HashMap<String, String>>();
//URL to get JSON Array
private static String url = "http://10.10.10.2/test/ff.php";
//JSON Node Names
//private static final String ca
private static final String TAG_CAT = "cate";
private static final String TAG_TITLE = "title";
private static final String TAG_EDID = "EdId";
private static final String TAG_DATE = "date";
JSONArray cate = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test_main);
oslist = new ArrayList<HashMap<String, String>>();
new JSONParse().execute();
}
private class JSONParse extends AsyncTask<String, String, JSONObject> {
private ProgressDialog pDialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
title = (TextView)findViewById(R.id.title);
EdId = (TextView)findViewById(R.id.edid);
date = (TextView)findViewById(R.id.date);
pDialog = new ProgressDialog(Guys.this);
pDialog.setMessage("Getting Data ...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected JSONObject doInBackground(String... args) {
String jn="ff'f'd";
JSONParser jParser = new JSONParser();
// Getting JSON from URL
JSONObject json = jParser.getJSONFromUrl(url);
json.toString();
return json;
}
@Override
protected void onPostExecute(JSONObject json) {
pDialog.dismiss();
try {
//json = json.substring(7);
// Getting JSON Array from URL
cate = json.getJSONArray(TAG_CAT);
for(int i = 0; i < cate.length(); i++){
JSONObject c = cate.getJSONObject(i);
// Storing JSON item in a Variable
String title = c.getString(TAG_TITLE);
String EdId = c.getString(TAG_EDID);
//String date = c.getString(TAG_API);
// Adding value HashMap key => value
HashMap<String, String> map = new HashMap<String, String>();
map.put(TAG_TITLE, title);
map.put(TAG_EDID, EdId);
//map.put(TAG_API, date);
oslist.add(map);
list=(ListView)findViewById(R.id.list);
ListAdapter adapter = new SimpleAdapter(Guys.this, oslist,
R.layout.list_v,
new String[] { TAG_TITLE}, new int[] {
R.id.title});
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(Guys.this, "You Clicked at "+oslist.get(+position).get("EdId"), Toast.LENGTH_SHORT).show();
Intent myIntent = new Intent(Guys.this, SimpleBrowser.class);
String idurl = oslist.get(+position).get("EdId");
String idsend = idurl;
myIntent.putExtra("id",idurl.toString());
Guys.this.startActivity(myIntent);
}
});
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}
公共类扩展活动{
列表视图列表;
文本视图标题;
文本视图EdId;
文本查看日期;
按钮Btngetdata;
//按钮nextac;
ArrayList oslist=新的ArrayList();
//获取JSON数组的URL
专用静态字符串url=”http://10.10.10.2/test/ff.php";
//JSON节点名称
//私有静态最终字符串ca
专用静态最终字符串标记_CAT=“cate”;
私有静态最终字符串标记_TITLE=“TITLE”;
私有静态最终字符串标记_EDID=“EDID”;
私有静态最终字符串标记_DATE=“DATE”;
JSONArray cate=null;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.test_main);
oslist=newarraylist();
新建JSONParse().execute();
}
私有类JSONParse扩展了异步任务{
私人对话;
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
title=(TextView)findViewById(R.id.title);
EdId=(TextView)findViewById(R.id.EdId);
日期=(TextView)findViewById(R.id.date);
pDialog=newprogressdialog(Guys.this);
setMessage(“获取数据…”);
pDialog.setUndeterminate(假);
pDialog.setCancelable(真);
pDialog.show();
}
@凌驾
受保护的JSONObject doInBackground(字符串…args){
字符串jn=“ff'f'd”;
JSONParser jParser=新的JSONParser();
//从URL获取JSON
JSONObject json=jParser.getJSONFromUrl(url);
json.toString();
返回json;
}
@凌驾
受保护的void onPostExecute(JSONObject json){
pDialog.disclose();
试一试{
//json=json.substring(7);
//从URL获取JSON数组
cate=json.getJSONArray(TAG_CAT);
对于(int i=0;ivalue
HashMap=newHashMap();
地图放置(标签标题、标题);
地图放置(标签EDID,EDID);
//地图放置(TAG_API,日期);
添加(地图);
list=(ListView)findViewById(R.id.list);
ListAdapter=new SimpleAdapter(Guys.this,oslist,
R.layout.list_v,
新字符串[]{TAG_TITLE},新int[]{
R.id.title});
list.setAdapter(适配器);
list.setOnItemClickListener(新的AdapterView.OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView父级、视图、,
内部位置,长id){
Toast.makeText(Guys.this,“您在“+oslist.get(+position.get”(“EdId”)、Toast.LENGTH_SHORT.show()处单击了“+oslist.get(+position.get”(“EdId”);
Intent myIntent=newintent(Guys.this,SimpleBrowser.class);
字符串idurl=oslist.get(+position.get(“EdId”);
字符串idsend=idurl;
myIntent.putExtra(“id”,idurl.toString());
伙计们。这个。星际触觉(我的意图);
}
});
}
}捕获(JSONException e){
e、 printStackTrace();
}
}
}
}
我认为问题在于生成json
字符串的系统,在生产者系统中,您要么需要将“
字符转义为\”
字符,要么使用”
字符 您可以使用gson类来解析json响应。
您需要创建一个类
class Data{
ArrayList<Data> cate;
getCateList(){
return cate;
}
String title;
String EdId;
String date;
}
InputStream source = retrieveStream(url);
Gson gson = new Gson();
Reader reader = new InputStreamReader(source);
Data response = gson.fromJson(reader, Data.class);
类数据{
ArrayList cate;
getCateList(){
返回美食;
}
字符串标题;
字符串EdId;
字符串日期;
}
InputStream源=检索流(url);
Gson Gson=新的Gson();
Reader Reader=新的InputStreamReader(源);
数据响应=gson.fromJson(reader,Data.class);
阅读以下教程:您的json无效!我很想知道为什么这个问题的编辑被批准,特别是因为它引入了新的打字错误,并通过删除“引号”中的“引号”来模糊问题。我无法从json对象中删除引号,因为这个json对象是在线的,我尝试创建一个android应用程序来解析来自这个网站的新闻,所以如果一些新闻有引用,我的json没有解析数据