Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/397.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在java中解析magento json_Java_Json_Magento - Fatal编程技术网

在java中解析magento json

在java中解析magento json,java,json,magento,Java,Json,Magento,我需要使用java解析json文件,以便在jsp文件中使用 例如,当我调用此URL时,在Magento中使用REST API: magento/api/rest/products?限制=2 我有这样的想法: { "16": {"entity_id":"16", "attribute_set_id":"38", "type_id":"simple", "sku":"n2610", "model":"2610", "dimension":"4.1 x 1.7 x 0.7 inches ", "des

我需要使用java解析json文件,以便在jsp文件中使用

例如,当我调用此URL时,在Magento中使用REST API: magento/api/rest/products?限制=2

我有这样的想法:

{
"16":
{"entity_id":"16",
"attribute_set_id":"38",
"type_id":"simple",
"sku":"n2610",
"model":"2610",
"dimension":"4.1 x 1.7 x 0.7 inches ",
"description":"The Nokia 2610 is",
"in_depth":"Integrated camera",
"activation_information":"Conditional $250 Equipment Discount Included",
"meta_keyword":"Nokia 2610, cell, phone, ",
"short_description":"The words \"entry level\" no longer mean \"low-end,\" especially when it comes to the Nokia 2610",
"custom_layout_update":"",
"status":"1",
"tax_class_id":"2",
"color":"24",
"visibility":"4",
"weight":"3.2000",
"price":"149.9900",
"cost":"20.0000",
"name":"Nokia 2610 Phone",
"manufacturer":"20",
"url_key":"nokia-2610-phone",
"meta_title":"Nokia 2610",
"meta_description":"Offering advanced media and calling features without breaking the bank, The Nokia 2610 is an easy to use",
"gift_message_available":"",
"options_container":"container2",
"custom_design":""},

"17":
{"entity_id":"17",
"attribute_set_id":"38",
"type_id":"simple",
"sku":"bb8100",
"model":"8100",
"dimension":"4.2 x 2 x 0.6 inches ",
"description":" Like the BlackBerry 7105t",
"in_depth":"1.3 mega pixel camera to capture those special moments",
"activation_information":"CONDITIONAL $250 Equipment Discount Included",
"meta_keyword":"Blackberry, 8100, pearl, cell, phone",
"short_description":"The BlackBerry 8100 Pearl is a departure from the form factor of previous BlackBerry devices",
"custom_layout_update":"",
"color":"23",
"status":"1",
"tax_class_id":"2",
"visibility":"4",
"weight":"15.2000",
"price":"349.9900",
"cost":"29.9900",
"name":"BlackBerry 8100 Pearl",
"manufacturer":"21",
"url_key":"blackberry-8100-pearl",
"meta_title":"BlackBerry 8100 Pearl",
"meta_description":"BlackBerry 8100 Pearl sports a large 240 x 260 screen",
"gift_message_available":"",
"options_container":"container2",
"custom_design":""}}
我需要提取一些信息:名称,价格,描述。。。但我不知道怎么做

我读了一些教程

问题在于,Magento Json文件与此文件非常不同和特殊,例如:

{
  "empID": 100,
  "name": "David",
  "permanent": false,
  "address": {
    "street": "BTM 1st Stage",
    "city": "Bangalore",
    "zipcode": 560100
  },
  "phoneNumbers": [
    123456,
    987654
  ],
  "role": "Manager",
  "cities": [
    "Los Angeles",
    "New York"
  ],
  "properties": {
    "age": "28 years",
    "salary": "1000 Rs"
  }
}

因为我们有“16”和“17”是“动态的”。

第一个json无效。
“描述”后缺少逗号:“像黑莓7105t”


这种类型的json是一个映射,因此16和17是映射中的键

然后,您可以在此处使用代码进行解码:

顺便说一句:我从文件中读取的是您的json字符串

publicstaticvoidmain(字符串[]args)抛出FileNotFoundException{
Gson Gson=新的Gson();
字符串json=readFromFile(新文件(“json.txt”);
HashMaps=gson.fromJson(json,newTypeToken(){}.getType());
System.out.println(maps.get(“16”).说明);
}
静态字符串readFromFile(文件文件)引发FileNotFoundException{
Scanner Scanner=新扫描仪(新文件输入流(文件));
StringBuilder=新的StringBuilder();
while(scanner.hasNext()){
append(scanner.nextLine());
}
scanner.close();
返回builder.toString();
}
静态类GObj{
公共int实体id;
公共int属性集id;
公众地位;
公共内部税类id;
公共int颜色;
公众能见度;
公共int制造商;
公共双权;
公开双价;
公共双重成本;
公共字符串类型\u id;
公共字符串sku;
公共字符串模型;
公共字符串维度;
公共字符串描述;
公共字符串深度;
公共字符串激活信息;
公共字符串meta_关键字;
公共字符串短_描述;
公共字符串自定义布局更新;
公共字符串名称;
公共字符串url_键;
公共字符串元标题;
公共字符串元描述;
公共字符串礼品\信息\可用;
公共字符串选项\u容器;
公共字符串自定义设计;
}
谢谢你,迈克。 最后,这里是我的java文件:

package main;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Scanner;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

public class Test {
    public static void main(String[] args) throws FileNotFoundException {
        Gson gson = new Gson();
       String json = readFromFile(new File("test.txt"));


        HashMap<String, GObj> maps = gson.fromJson(json, new TypeToken<HashMap<String, GObj>>(){}.getType());
        System.out.println(maps.size());
        System.out.println(maps.keySet());
        Iterator it = maps.keySet().iterator();
        while (it.hasNext()) {
            System.out.println(maps.get(it.next()).price);
        }




    }

    static String readFromFile(File file) throws FileNotFoundException {
        Scanner scanner = new Scanner(new FileInputStream(file));
        StringBuilder builder = new StringBuilder();
        while (scanner.hasNext()) {
            builder.append(scanner.nextLine());
        }
        scanner.close();
        return builder.toString();
    }


    static class GObj {
        public String    entity_id;
        public String    attribute_set_id;
        public String    status;
        public String    tax_class_id;
        public String    color;
        public String    visibility;
        public String    manufacturer;
        public String weight;
        public String price;
        public String cost;
        public String type_id;
        public String sku;
        public String model;
        public String dimension;
        public String description;
        public String in_depth;
        public String activation_information;
        public String meta_keyword;
        public String short_description;
        public String custom_layout_update;
        public String name;
        public String url_key;
        public String meta_title;
        public String meta_description;
        public String gift_message_available;
        public String options_container;
        public String custom_design;
    }
}

您的第一个json无效。。“描述”后缺少逗号:“喜欢黑莓7105t”谢谢。我纠正了它。它解决了你的问题吗?不,它没有解决它。我用它来动态访问:迭代器it=maps.keySet().Iterator();while(it.hasNext()){System.out.println(maps.get(it.next()).price);}
package main;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Scanner;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

public class Test {
    public static void main(String[] args) throws FileNotFoundException {
        Gson gson = new Gson();
       String json = readFromFile(new File("test.txt"));


        HashMap<String, GObj> maps = gson.fromJson(json, new TypeToken<HashMap<String, GObj>>(){}.getType());
        System.out.println(maps.size());
        System.out.println(maps.keySet());
        Iterator it = maps.keySet().iterator();
        while (it.hasNext()) {
            System.out.println(maps.get(it.next()).price);
        }




    }

    static String readFromFile(File file) throws FileNotFoundException {
        Scanner scanner = new Scanner(new FileInputStream(file));
        StringBuilder builder = new StringBuilder();
        while (scanner.hasNext()) {
            builder.append(scanner.nextLine());
        }
        scanner.close();
        return builder.toString();
    }


    static class GObj {
        public String    entity_id;
        public String    attribute_set_id;
        public String    status;
        public String    tax_class_id;
        public String    color;
        public String    visibility;
        public String    manufacturer;
        public String weight;
        public String price;
        public String cost;
        public String type_id;
        public String sku;
        public String model;
        public String dimension;
        public String description;
        public String in_depth;
        public String activation_information;
        public String meta_keyword;
        public String short_description;
        public String custom_layout_update;
        public String name;
        public String url_key;
        public String meta_title;
        public String meta_description;
        public String gift_message_available;
        public String options_container;
        public String custom_design;
    }
}
2
[17, 16]
349.9900
149.9900